// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.
// Author:					Joe Audette
// Created:				    2004-07-04
// Last Modified:			2011-04-05
// 
// 04/30/2005	Dean Brettle Provided a better handling of proxy settings
//				in generating the base path for site links
//				
// 01/18/2007   Alexander Yushchenko introduced SetButtonAccessKey() function
// 
// 04/05/2007   Alexander Yushchenko introduced Redirect* and AllowOnly* functions
// 2011-02-27 Joe Audette added method IsSecureRequest()
//  


using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Net;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using log4net;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Net;
using mojoPortal.Web.UI;
using mojoPortal.Web.Controls;
using mojoPortal.Web.Framework;
using mojoPortal.Web.Editor;
using Resources;

namespace mojoPortal.Web
{
    /// <summary>
    ///
    /// </summary>
    public static class SiteUtils
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(SiteUtils));


        public static bool RunningOnMono()
        {
            try
            {
                Type t = Type.GetType("Mono.Runtime");
                if (t != null) { return true; }
            }
            catch (Exception) { }


            return false;
        }

        /// <summary>
        /// compares 2 urls, if running on Mono does a case sensitive match
        /// else it does caseinsenitive match
        /// returns false if either string isnullorempty
        /// </summary>
        /// <param name="url1"></param>
        /// <param name="url2"></param>
        /// <returns></returns>
        public static bool UrlsMatch(string url1, string url2)
        {
            if (string.IsNullOrEmpty(url1)) { return false; }
            if (string.IsNullOrEmpty(url2)) { return false; }

            if (RunningOnMono())
            {
                return (url1 == url2);
            }

            return string.Equals(url1, url2, StringComparison.InvariantCultureIgnoreCase);

        }

        public static String SuggestFriendlyUrl(
            String pageName,
            SiteSettings siteSettings)
        {
            String friendlyUrl = CleanStringForUrl(pageName);
            if (WebConfigSettings.AlwaysUrlEncode)
            {
                friendlyUrl = HttpUtility.UrlEncode(friendlyUrl);
            }
            

            switch (siteSettings.DefaultFriendlyUrlPattern)
            {
                case SiteSettings.FriendlyUrlPattern.PageNameWithDotASPX:
                    friendlyUrl += ".aspx";
                    break;

            }

            int i = 1;
            while (FriendlyUrl.Exists(siteSettings.SiteId, friendlyUrl))
            {
                friendlyUrl = i.ToString() + friendlyUrl;
            }



            if (WebConfigSettings.ForceFriendlyUrlsToLowerCase) { return friendlyUrl.ToLower(); }

            return friendlyUrl;
        }

        public static string RemoveInvalidUrlChars(string input)
        {
            return input.Replace(":", string.Empty).Replace("?", string.Empty).Replace("&", "-").Replace("#", string.Empty);
        }

        public static string RemoveQuotes(string input)
        {
            string outputString = input.Replace("\"", string.Empty).Replace("'", string.Empty);

            
            return outputString;

        }

        public static String CleanStringForUrl(String input)
        {
            String outputString = RemovePunctuation(input.Replace("&", "-")).Replace(" - ", "-").Replace("--", "-").Replace(" ", "-").Replace("/", String.Empty).Replace("\"", String.Empty).Replace("'", String.Empty).Replace("#", String.Empty).Replace("~", String.Empty).Replace("`", String.Empty).Replace("@", String.Empty).Replace("$", String.Empty).Replace("*", String.Empty).Replace("^", String.Empty).Replace("(", String.Empty).Replace(")", String.Empty).Replace("+", String.Empty).Replace("=", String.Empty).Replace("%", String.Empty).Replace(">", String.Empty).Replace("<", String.Empty);

            if (WebConfigSettings.UseClosestAsciiCharsForUrls) { return outputString.ToAsciiIfPossible(); }

            return outputString;

        }

        private static String RemovePunctuation(String input)
        {
            String outputString = String.Empty;
            if (input != null)
            {
                outputString = input.Replace(".", String.Empty).Replace(",", String.Empty).Replace(":", String.Empty).Replace("?", String.Empty).Replace("!", String.Empty).Replace(";", String.Empty).Replace("&", String.Empty).Replace("{", String.Empty).Replace("}", String.Empty).Replace("[", String.Empty).Replace("]", String.Empty);
            }
            return outputString;
        }

        

        /// <summary>
        /// You should pass your editor to this method during pre-init or init
        /// </summary>
        /// <param name="editor"></param>
        public static void SetupNewsletterEditor(EditorControl editor)
        {
            if (HttpContext.Current == null) { return; }
            if (HttpContext.Current.Request == null) { return; }
            if (editor == null) return;

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return; }

            string providerName = siteSettings.NewsletterEditor;

            string loweredBrowser = string.Empty;

            if (HttpContext.Current.Request.UserAgent != null)
            {
                loweredBrowser = HttpContext.Current.Request.UserAgent.ToLower();
            }

            if (
                (loweredBrowser.Contains("iphone"))
                && (WebConfigSettings.ForcePlainTextInIphone)
                )
            {
                providerName = "TextAreaProvider";
            }

            if (
                (loweredBrowser.Contains("android"))
                && (WebConfigSettings.ForcePlainTextInAndroid)
                )
            {
                providerName = "TextAreaProvider";
            }

            string siteRoot = null;
            if (siteSettings.SiteFolderName.Length > 0)
            {
                siteRoot = siteSettings.SiteRoot;
            }
            if (siteRoot == null) siteRoot = WebUtils.GetSiteRoot();

            editor.ProviderName = providerName;
            editor.WebEditor.SiteRoot = siteRoot;
            
            CultureInfo defaultCulture = SiteUtils.GetDefaultCulture();
            if (defaultCulture.TextInfo.IsRightToLeft)
            {
                editor.WebEditor.TextDirection = Direction.RightToLeft;
            }



        }

        public static string GetIP4Address()
        {
            string ip4Address = string.Empty;
            if (HttpContext.Current == null) { return ip4Address; }
            if (HttpContext.Current.Request == null) { return ip4Address; }

            if (WebConfigSettings.ClientIpServerVariable.Length > 0)
            {
                if (HttpContext.Current.Request.ServerVariables[WebConfigSettings.ClientIpServerVariable] != null)
                {
                    return HttpContext.Current.Request.ServerVariables[WebConfigSettings.ClientIpServerVariable];
                }
            }

            if (HttpContext.Current.Request.UserHostAddress == null) { return ip4Address; }

            try
            {
                IPAddress ip = IPAddress.Parse(HttpContext.Current.Request.UserHostAddress);
                if (ip.AddressFamily.ToString() == "InterNetwork") { return ip.ToString(); }
            }
            catch (FormatException)
            { }
            catch (ArgumentNullException) { }

            try
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        ip4Address = IPA.ToString();
                        break;
                    }
                }
            }
            catch (ArgumentException)
            { }
            catch (System.Net.Sockets.SocketException) { }

            if (ip4Address != string.Empty)
            {
                return ip4Address;
            }

            //this part makes no sense it would get the local server ip address
            try
            {
                foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (IPA.AddressFamily.ToString() == "InterNetwork")
                    {
                        ip4Address = IPA.ToString();
                        break;
                    }
                }
            }
            catch (ArgumentException)
            { }
            catch (System.Net.Sockets.SocketException) { }

            return ip4Address;
        }

        public static string BuildStylesListForTinyMce()
        {
            StringBuilder styles = new StringBuilder();

            string comma = string.Empty;

            if (WebConfigSettings.AddSystemStyleTemplatesAboveSiteTemplates)
            {
                styles.Append("FloatPanel=floatpanel,Image on Right=floatrightimage,Image on Left=floatleftimage");
                comma = ",";
            }

            
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings != null)
            {
                using (IDataReader reader = ContentStyle.GetAllActive(siteSettings.SiteGuid))
                {
                    while (reader.Read())
                    {
                        styles.Append(comma + reader["Name"].ToString() + "=" + reader["CssClass"].ToString());
                        comma = ",";
                    }
                }

            }

            if (WebConfigSettings.AddSystemStyleTemplatesBelowSiteTemplates)
            {
                styles.Append(comma + "FloatPanel=floatpanel,Image on Right=floatrightimage,Image on Left=floatleftimage");
            }

            return styles.ToString();
        }

        public static bool IsImageFileExtension(string fileExtension)
        {
            List<string> allowedExtensions = new List<string>();
            allowedExtensions.Add(".jpg");
            allowedExtensions.Add(".jpeg");
            allowedExtensions.Add(".png");
            allowedExtensions.Add(".gif");

            foreach (string ext in allowedExtensions)
            {
                if (string.Equals(fileExtension, ext, StringComparison.InvariantCultureIgnoreCase)) { return true; }
            }

            return false;

        }

        public static string ImageFileExtensions()
        {
            return ".gif|.jpg|.jpeg|.png";
        }

        public static List<string> ExtractUrls(string html)
        {
            string urlRegex = WebConfigSettings.UrlRegex;

            List<string> urls = new List<string>();

            MatchCollection matches = Regex.Matches(html, urlRegex);
            foreach (Match m in matches)
            {
                urls.Add(SecurityHelper.RemoveAngleBrackets(RemoveQuotes(m.Value)));
            }

            return urls;
        }

        public static bool IsAllowedMediaFile(this FileInfo fileInfo)
        {
            List<string> allowedExtensions = StringHelper.SplitOnPipes(WebConfigSettings.AllowedMediaFileExtensions);
            foreach (string ext in allowedExtensions)
            {
                if (string.Equals(fileInfo.Extension, ext, StringComparison.InvariantCultureIgnoreCase)) { return true; }
            }

            return false;

        }

        public static bool IsAllowedUploadBrowseFile(this FileInfo fileInfo, string allowedExtensions)
        {
            List<string> allowed = StringHelper.SplitOnPipes(allowedExtensions);
            foreach (string ext in allowed)
            {
                if (string.Equals(fileInfo.Extension, ext, StringComparison.InvariantCultureIgnoreCase)) { return true; }
            }

            return false;
        }

        public static bool IsAllowedUploadBrowseFile(string fileExtension, string allowedExtensions)
        {
            List<string> allowed = StringHelper.SplitOnPipes(allowedExtensions);
            foreach (string ext in allowed)
            {
                if (string.Equals(fileExtension, ext, StringComparison.InvariantCultureIgnoreCase)) { return true; }
            }

            return false;
        }
        

        


        public static void SetButtonAccessKey(Button button, string accessKey)
        {
            if (!WebConfigSettings.UseShortcutKeys) return;

            button.AccessKey = accessKey;
            button.Text += GetButtonAccessKeyPostfix(accessKey);
        }


        public static string GetButtonAccessKeyPostfix(string accessKey)
        {
            if (HttpContext.Current == null) return String.Empty;

            string browser = HttpContext.Current.Request.Browser.Browser;
            string browserAccessKey = browser.ToLower().Contains("opera")
                                          ? AccessKeys.BrowserOperaAccessKey
                                          : AccessKeys.BrowserAccessKey;

            return string.Format(CultureInfo.InvariantCulture," [{0}+{1}]", browserAccessKey, accessKey);
        }

        //public static string RolesThatCanUploadAndBrowse()
        //{
        //    // TODO: make this site specific

        //    return WebConfigSettings.RolesThatCanUploadAndBrowse;


        //}

        //public static string RolesThatCanManageUsers()
        //{
        //    // TODO: make this site specific

        //    return WebConfigSettings.RolesThatCanManageUsers;


        //}

        /// <summary>
        /// this method is deprecated
        /// </summary>
        [Obsolete("This method is obsolete. You should use if(!Request.IsAuthenticated) SiteUtils.RedirectToLogin(PageorControl); return;")]
        public static void AllowOnlyAuthenticated()
        {
            if (HttpContext.Current == null) return;
            if (!HttpContext.Current.Request.IsAuthenticated) RedirectToLoginPage();
        }

        [Obsolete("This method is obsolete. You should use if(!Request.IsAuthenticated) SiteUtils.RedirectToLogin(PageorControl); return;")]
        public static void AllowOnlyAuthenticated(Control pageOrControl)
        {
            if (HttpContext.Current == null) return;
            if (!HttpContext.Current.Request.IsAuthenticated) RedirectToLoginPage(pageOrControl);
        }

        [Obsolete("This method is obsolete. You should use if(!WebUser.IsAdmin) SiteUtils.RedirectToAccessDenied(PageorControl); return;")]
        public static void AllowOnlyAdmin()
        {
            if (HttpContext.Current == null) return;
            AllowOnlyAuthenticated();
            if (!HttpContext.Current.Request.IsAuthenticated) return;
            if (!WebUser.IsAdmin) RedirectToEditAccessDeniedPage();
        }

        [Obsolete("This method is obsolete. You should use if(!WebUser.IsAdminOrRoleAdmin) SiteUtils.RedirectToAccessDenied(PageorControl); return;")]
        public static void AllowOnlyAdminAndRoleAdmin()
        {
            if (HttpContext.Current == null) return;
            AllowOnlyAuthenticated();
            if (!HttpContext.Current.Request.IsAuthenticated) return;
            if ((!WebUser.IsAdmin) && (!WebUser.IsRoleAdmin))
            {
                RedirectToEditAccessDeniedPage();
            }
        }

        //public static void AllowOnlyAdminAndNewsletterAdmin()
        //{
        //    if (HttpContext.Current == null) return;
        //    AllowOnlyAuthenticated();
        //    if (!HttpContext.Current.Request.IsAuthenticated) return;
        //    if ((!WebUser.IsAdmin) && (!WebUser.IsNewsletterAdmin))
        //    {
        //        RedirectToEditAccessDeniedPage();
        //    }
        //}

        [Obsolete("This method is obsolete. You should use if(!WebUser.IsAdminOrContentAdmin) SiteUtils.RedirectToAccessDenied(PageorControl); return;")]
        public static void AllowOnlyAdminAndContentAdmin()
        {
            if (HttpContext.Current == null) return;
            AllowOnlyAuthenticated();
            if (!HttpContext.Current.Request.IsAuthenticated) return;
            if (!WebUser.IsAdminOrContentAdmin) RedirectToEditAccessDeniedPage();
        }

        [Obsolete("This method is obsolete. You should use if(!WebUser.IsAdminOrContentAdminOrRoleAdmin) SiteUtils.RedirectToAccessDenied(PageorControl); return;")]
        public static void AllowOnlyAdminAndContentAdminAndRoleAdmin()
        {
            if (HttpContext.Current == null) return;
            AllowOnlyAuthenticated();
            if (!HttpContext.Current.Request.IsAuthenticated) return;
            if (!WebUser.IsAdminOrContentAdminOrRoleAdmin) RedirectToEditAccessDeniedPage();
        }

        [Obsolete("This method is obsolete. You should use RedirectToLoginPage(pageOrControl); return;")]
        public static void RedirectToLoginPage()
        {
            HttpContext.Current.Response.Redirect
                (string.Format(CultureInfo.InvariantCulture,"{0}" + GetLoginRelativeUrl() + "?returnurl={1}",
                               GetNavigationSiteRoot(),
                               HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl)),
                 true);
        }

        public static void RedirectToLoginPage(Control pageOrControl)
        {
            string redirectUrl 
                = string.Format(CultureInfo.InvariantCulture,"{0}" + GetLoginRelativeUrl() + "?returnurl={1}",
                               GetNavigationSiteRoot(),
                               HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl));
            
            WebUtils.SetupRedirect(pageOrControl, redirectUrl);
        }

        public static void RedirectToLoginPage(Control pageOrControl, string returnUrl)
        {
            string redirectUrl
                = string.Format(CultureInfo.InvariantCulture, "{0}" + GetLoginRelativeUrl() + "?returnurl={1}",
                               GetNavigationSiteRoot(),
                               HttpUtility.UrlEncode(returnUrl));

            WebUtils.SetupRedirect(pageOrControl, redirectUrl);
        }

        public static void RedirectToLoginPage(Control pageOrControl, bool useHardRedirect)
        {
            if (!useHardRedirect)
            {
                RedirectToLoginPage(pageOrControl);
                return;
            }

            string redirectUrl
                = string.Format(CultureInfo.InvariantCulture, "{0}" + GetLoginRelativeUrl() + "?returnurl={1}",
                               GetNavigationSiteRoot(),
                               HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl));

            pageOrControl.Page.Response.Redirect(redirectUrl);

            //WebUtils.SetupRedirect(pageOrControl, redirectUrl);
        }

        public static string GetLoginRelativeUrl()
        {
            if (ConfigurationManager.AppSettings["LoginPageRelativeUrl"] != null)
                return ConfigurationManager.AppSettings["LoginPageRelativeUrl"];

            return "/Secure/Login.aspx";

        }

        public static void RedirectToUrl(string url)
        {
            if (HttpContext.Current == null) { return; }
          
            HttpContext.Current.Response.RedirectLocation = url;
            HttpContext.Current.Response.StatusCode = 302;
            HttpContext.Current.Response.StatusDescription = "Redirecting to " + url;
            HttpContext.Current.Response.Write("Redirecting to " + url);
            HttpContext.Current.ApplicationInstance.CompleteRequest();

        }

        public static void RedirectToEditAccessDeniedPage()
        {
            HttpContext.Current.Response.Redirect(GetNavigationSiteRoot() + "/EditAccessDenied.aspx", true);
            //RedirectToUrl(GetNavigationSiteRoot() + "/EditAccessDenied.aspx");
        }


        public static void RedirectToAccessDeniedPage()
        {
            //HttpContext.Current.Response.Redirect(GetNavigationSiteRoot() + "/AccessDenied.aspx", false);
            RedirectToUrl(GetNavigationSiteRoot() + "/AccessDenied.aspx");
        }

        public static void RedirectToAccessDeniedPage(Control pageOrControl)
        {
            string redirectUrl
                = GetNavigationSiteRoot() + "/AccessDenied.aspx";

            WebUtils.SetupRedirect(pageOrControl, redirectUrl);
        }

        public static void RedirectToAccessDeniedPage(Control pageOrControl, bool useHardRedirect)
        {
            if (!useHardRedirect)
            {
                RedirectToAccessDeniedPage(pageOrControl);
                return;
            }

            string redirectUrl
                = GetNavigationSiteRoot() + "/AccessDenied.aspx";

            pageOrControl.Page.Response.Redirect(redirectUrl);

            //WebUtils.SetupRedirect(pageOrControl, redirectUrl);
        }


        public static void RedirectToDefault()
        {
            //HttpContext.Current.Response.Redirect(GetNavigationSiteRoot() + "/default.aspx", false);
            RedirectToUrl(GetNavigationSiteRoot() + "/Default.aspx");
        }


        public static void SetFormAction(Page page, string action)
        {
            page.Form.Action = action;
        }

        public static string GetMasterPage(
            Page page,
            SiteSettings siteSettings,
            bool allowOverride)
        {
            string skinFolder = "~/App_MasterPages/";
            string masterPage = "layout.Master";
            PageSettings currentPage = CacheHelper.GetCurrentPage();

            if (
                (HttpContext.Current != null)
                && (page != null)
                && (siteSettings != null)
                )
            {
                skinFolder = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/";
                masterPage = siteSettings.Skin + "/layout.Master";

                // implement user skins
                if ((siteSettings.AllowUserSkins) && (HttpContext.Current.Request.IsAuthenticated))
                {
                    SiteUser currentUser = GetCurrentSiteUser();

                    if ((currentUser != null) && (currentUser.Skin.Length > 0))
                    {
                        string userSkin = currentUser.Skin + "/layout.Master";

                        if (File.Exists(HttpContext.Current.Server.MapPath(skinFolder + userSkin)))
                        {
                            masterPage = userSkin;
                        }
                    }
                }
                
                // implement per page skins
                if (
                    (allowOverride)
                    && (siteSettings.AllowPageSkins)
                    )
                {
                    if (
                        (currentPage != null)
                        && (currentPage.Skin.Length > 0)
                        )
                    {
                        masterPage = currentPage.Skin + "/layout.Master";
                    }
                }

               
                // implement skin preview using querystring param
                if (HttpContext.Current.Request.Params.Get("skin") != null)
                {
                    string previewSkin = SanitizeSkinParam(HttpContext.Current.Request.Params.Get("skin")) + "/layout.Master";

                    if (File.Exists(HttpContext.Current.Server.MapPath(skinFolder + previewSkin)))
                    {
                        masterPage = previewSkin;
                    }
                }

                
            }

            if (page is mojoBasePage)
            {
                if (((mojoBasePage)page).IsMobileDevice)
                {
                    if (WebConfigSettings.UseMobileSpecificSkin)
                    {
                        if (WebConfigSettings.MobilePhoneSkin.Length > 0)
                        {
                            masterPage = WebConfigSettings.MobilePhoneSkin + "/layout.Master";

                        }

                    }
                }
            }

            //log.Info("set master page to " + skinFolder + masterPage);
           
            return skinFolder + masterPage;

        }

        public static string GetSkinPreviewParam(SiteSettings siteSettings)
        {
            if (siteSettings == null) { return string.Empty; }
            // implement skin preview using querystring param
            if (HttpContext.Current.Request.Params.Get("skin") != null)
            {
                string skinFolder = "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/"; ;
                string previewSkin = SanitizeSkinParam(HttpContext.Current.Request.Params.Get("skin")) ;

                try
                {
                    if (File.Exists(HttpContext.Current.Server.MapPath(skinFolder + previewSkin + "/layout.Master")))
                    {
                        return previewSkin;
                    }
                }
                catch (HttpException) { }
            }

            return string.Empty;
        }

        //public static void SetMasterPage(
        //    Page page, 
        //    SiteSettings siteSettings,
        //    bool allowOverride)
        //{
        //    String skinFolder;
        //    String skinName;
        //    PageSettings currentPage = CacheHelper.GetCurrentPage();

        //    if (
        //        (HttpContext.Current != null)
        //        &&(page != null)
        //        &&(siteSettings != null)
        //        )
        //    {
               
        //        skinFolder = "~/Data/Sites/" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/skins/";
        //        skinName = siteSettings.Skin.Replace(".ascx", "") + "/layout.Master";

        //        // implement user skins
        //        if (siteSettings.AllowUserSkins)
        //        {
        //            string skinCookieName = "mojoUserSkin" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture);
        //            if (CookieHelper.CookieExists(skinCookieName))
        //            {
        //                string cookieValue = CookieHelper.GetCookieValue(skinCookieName);
        //                if (File.Exists(HttpContext.Current.Server.MapPath(skinFolder + cookieValue.Replace(".ascx", "") + "/layout.Master")))
        //                {
        //                    skinName = cookieValue.Replace(".ascx", "") + "/layout.Master";

        //                }

        //            }
        //        }

        //        // implement per page skins
        //        if (
        //            (allowOverride)
        //            &&(siteSettings.AllowPageSkins)
        //            )
        //        {
        //            if (
        //                (currentPage != null)
        //                &&(currentPage.Skin.Length > 0)
        //                )
        //            {
        //                if (File.Exists(HttpContext.Current.Server.MapPath(skinFolder + currentPage.Skin.Replace(".ascx", "") + "/layout.Master")))
        //                {
        //                    skinName = currentPage.Skin.Replace(".ascx", "") + "/layout.Master";
        //                }
        //            }
        //        }


        //        // implement skin preview using querystring param
        //        if (HttpContext.Current.Request.Params.Get("skin") != null)
        //        {
        //            string previewSkin = SanitizeSkinParam(HttpContext.Current.Request.Params.Get("skin"));

        //            if (!previewSkin.EndsWith("/layout.ascx"))
        //            {
        //                previewSkin += "/layout.Master";
        //            }


        //            if (File.Exists(HttpContext.Current.Server.MapPath(skinFolder + previewSkin)))
        //            {
        //                skinName = previewSkin;

        //            }

        //        }
        //    }
        //    else
        //    {
        //        // hard coded only at design time, at runtime we get this from siteSettings
        //        skinFolder = "~/Data/Sites/1/skins/";
        //        skinName = "styleshout-techmania/layout.Master";

        //    }

        //    page.MasterPageFile = skinFolder + skinName;

        //}

        public static string SanitizeSkinParam(string skinName)
        {
            if (string.IsNullOrEmpty(skinName)) { return skinName; }

            // protected against this xss attack
            // ?skin=1%00'"><ScRiPt%20%0a%0d>alert(403326057258)%3B</ScRiPt>
            return skinName.Replace("%", string.Empty).Replace(" ", string.Empty).Replace(">", string.Empty).Replace("<", string.Empty).Replace("'", string.Empty).Replace("\"", string.Empty) ;
            

        }

        public static string GetMyPageMasterPage(SiteSettings siteSettings)
        {
            if (siteSettings == null)
            {
                return "~/App_MasterPages/layout.Master";
            }

            if (siteSettings.MyPageSkin.Length > 0)
            {
                return "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/" + siteSettings.MyPageSkin + "/layout.Master";
            }

            return "~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/" + siteSettings.Skin + "/layout.Master";

        }

        //public static void SetMasterPage(
        //    Page page,
        //    SiteSettings siteSettings,
        //    string skinName)
        //{
            
        //    if(HttpContext.Current == null) { return; }
        //    if (page == null) { return; }
        //    if (siteSettings == null) { return; }
        //    if(string.IsNullOrEmpty(skinName)) { return;}

        //    string masterPagePath = "~/Data/Sites/" 
        //        + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture)
        //        + "/skins/" + SanitizeSkinParam(skinName) + "/layout.Master";

        //    page.MasterPageFile = masterPagePath;

        //}


        public static void SetSkinCookie(SiteUser siteUser)
        {
            if (siteUser == null) return;
            string skinCookieName = "mojoUserSkin" + siteUser.SiteId.ToString(CultureInfo.InvariantCulture);
            HttpCookie cookie = HttpContext.Current.Request.Cookies[skinCookieName];
            bool setCookie = (cookie == null) || (cookie.Value != siteUser.Skin);
            if (setCookie)
            {
                HttpCookie userSkinCookie = new HttpCookie(skinCookieName, siteUser.Skin);
                userSkinCookie.Expires = DateTime.Now.AddYears(1);
                HttpContext.Current.Response.Cookies.Add(userSkinCookie);

            }
        }


        public static void SetDisplayNameCookie(string displayName)
        {
            if (String.IsNullOrEmpty(displayName)) return;

            HttpCookie cookie = HttpContext.Current.Request.Cookies["DisplayName"];
            bool setCookie = (cookie == null) || (cookie.Value != displayName);
            if (setCookie)
            {
                HttpCookie userNameCookie = new HttpCookie("DisplayName", HttpUtility.HtmlEncode(displayName));
                userNameCookie.Expires = DateTime.Now.AddYears(1);
                HttpContext.Current.Response.Cookies.Add(userNameCookie);
            }
        }

        public static List<ContentTemplate> GetSystemContentTemplates()
        {
            List<ContentTemplate> templates = new List<ContentTemplate>();

            ContentTemplate t = ContentTemplate.GetEmpty();
            //jQuery accordion
            t.Guid = new Guid("e110400d-c92d-4d78-a830-236f584af115");
            t.Body = "<p>Paragraph before the accordion</p><div class=\"mojo-accordion\"><h3><a href=\"#\">Section 1</a></h3><div><p>Section 1 content.</p></div><h3><a href=\"#\">Section 2</a></h3><div><p>Section 2 content</p></div><h3><a href=\"#\">Section 3</a></h3><div><p>Section 3 content</p></div><h3><a href=\"#\">Section 4</a></h3><div><p>Section 4 content</p></div></div><p>Paragraph after the accordion</p>";
            t.Title = Resource.TemplatejQueryAccordionTitle;
            t.ImageFileName = "jquery-accordion.gif";
            templates.Add(t);

            t = ContentTemplate.GetEmpty();
            //jQuery Accordion NoHeight
            t.Guid = new Guid("08e2a92f-d346-416b-b37b-bd82acf51514");
            t.Body = "<p>Paragraph before the accordion</p><div class=\"mojo-accordion-nh\"><h3><a href=\"#\">Section 1</a></h3><div><p>Section 1 content.</p></div><h3><a href=\"#\">Section 2</a></h3><div><p>Section 2 content</p></div><h3><a href=\"#\">Section 3</a></h3><div><p>Section 3 content</p></div><h3><a href=\"#\">Section 4</a></h3><div><p>Section 4 content</p></div></div><p>Paragraph after the accordion</p>";
            t.Title = Resource.TemplatejQueryAccordionNoAutoHeightTitle;
            t.ImageFileName = "jquery-accordion.gif";
            templates.Add(t);

            t = ContentTemplate.GetEmpty();
            //jQuery Tabs
            t.Guid = new Guid("7efaeb03-a1f9-4b08-9ffd-46237ba993b0");
            t.Body = "<p>Paragraph before the tabs</p><div class=\"mojo-tabs\"><ul><li><a href=\"#tab1\">Tab 1</a></li><li><a href=\"#tab2\">Tab 2</a></li><li><a href=\"#tab3\">Tab 3</a></li></ul><div id=\"tab1\"><p>Tab 1 content</p></div><div id=\"tab2\"><p>Tab 2 content</p></div><div id=\"tab3\"><p>Tab 3 content</p></div></div><p>Paragraph after the tabs</p>";
            t.Title = Resource.TemplatejQueryTabsTitle;
            t.ImageFileName = "jquerytabs.gif";
            templates.Add(t);

            if (WebConfigSettings.IncludeFaqContentTemplate) //just in case anyone wants to remove this one
            {
                t = ContentTemplate.GetEmpty();
                //jQuery FAQ
                t.Guid = new Guid("ad5f5b63-d07a-4e6b-bbd5-2b6201743dab");
                t.Body = "<h3>Example FAQ</h3><dl class=\"faqs\"><dt>Is this the first question?</dt><dd>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.&nbsp;</dd><dt>This must be the second question.</dt><dd>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.</dd><dt>And what about the third question?</dt><dd>Nam eget dui. Etiam rhoncus. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.</dd></dl>";
                t.Title = Resource.TemplatejQueryFAQTitle;
                t.ImageFileName = "faq.jpg";
                templates.Add(t);
            }

            if (WebConfigSettings.AlwaysLoadYuiTabs)
            {
                t = ContentTemplate.GetEmpty();
                //Yui Tabs
                t.Guid = new Guid("046dae46-5301-45a5-bcbf-0b87c2d9e919");
                t.Body = "<p>Paragraph before the tabs</p><div class=\"yui-skin-sam\"><div class=\"yui-navset\"><ul class=\"yui-nav\"><li class=\"selected\"><a href=\"#tab1\"><em>Tab One Label</em></a></li><li><a href=\"#tab2\"><em>Tab Two Label</em></a></li><li><a href=\"#tab3\"><em>Tab Three Label</em></a></li></ul><div class=\"yui-content\"><div id=\"tab1\"><p>Tab One Content</p></div><div id=\"tab2\"><p>Tab Two Content</p></div><div id=\"tab3\"><p>Tab Three Content</p></div></div></div></div><p>Paragraph after the tabs</p>";
                t.Title = Resource.TemplateYUITabsTitle;
                t.ImageFileName = "yui-tabs.gif";
                templates.Add(t);

            }

            t = ContentTemplate.GetEmpty();
            //2 columns over 1 
            t.Guid = new Guid("cfb9e9c4-b740-42f5-8c16-1957b536b8e9");
            t.Body = "<div class=\"floatpanel\"><div class=\"floatpanel section\" style=\"width: 46%;\"><h3>Lorem Ipsum</h3><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent varius varius est, id dictum lectus aliquet non. Fusce laoreet auctor facilisis. Nullam eget tortor at leo pellentesque pellentesque. Nunc tortor neque, elementum varius pretium sit amet, vulputate at erat. Duis nec nisi mauris, in gravida sapien.</p></div><div class=\"floatpanel section\" style=\"width: 46%;\"><h3>Duis a Mauris</h3><p>Duis a mauris non felis dapibus cursus. Aliquam eu dignissim purus. Donec at orci vitae sem laoreet molestie sed eu urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus magna velit, fringilla egestas vehicula at, adipiscing eget augue. Suspendisse porta, tellus id consequat volutpat.</p></div></div><div class=\"clear section\"><h3>Aenean?</h3><p>Aenean at urna nibh. Aliquam euismod tortor ut mauris eleifend ut vehicula neque convallis. Aenean dui orci, luctus non aliquet eu, semper non arcu. Aliquam tincidunt metus at ligula fringilla ornare. Praesent euismod, lacus vel condimentum convallis, massa quam auctor nisl, ut egestas felis sapien eget augue. Etiam eleifend auctor nunc, id facilisis ante ultrices in. Integer sagittis augue a tortor luctus ut tristique metus sagittis.</p></div><p>new paragraph</p>";
            t.Title = Resource.Template2ColumnsOver1ColumnTitle;
            t.ImageFileName = "columns2over1.gif";
            templates.Add(t);

            t = ContentTemplate.GetEmpty();
            //3 columns over 1
            t.Guid = new Guid("9ac79a8d-7dfd-4485-af3c-b8fdf256bbb8");
            t.Body = "<div class=\"floatpanel\"><div class=\"floatpanel section\" style=\"width: 31%;\"><h3>Lorem Ipsum</h3><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent varius varius est, id dictum lectus aliquet non. Fusce laoreet auctor facilisis. Nullam eget tortor at leo pellentesque pellentesque. Nunc tortor neque, elementum varius pretium sit amet, vulputate at erat. Duis nec nisi mauris, in gravida sapien.</p></div><div class=\"floatpanel section\" style=\"width: 31%;\"><h3>Duis a Mauris</h3><p>Duis a mauris non felis dapibus cursus. Aliquam eu dignissim purus. Donec at orci vitae sem laoreet molestie sed eu urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus magna velit, fringilla egestas vehicula at, adipiscing eget augue. Suspendisse porta, tellus id consequat volutpat.</p></div><div class=\"floatpanel section\" style=\"width: 31%;\"><h3>Vivamus Tristique!</h3><p>Vivamus tristique purus eget nisl sollicitudin varius. Praesent turpis sapien, imperdiet ut vehicula pretium, tristique nec mauris. Quisque eget lacus mi. Quisque adipiscing velit euismod enim venenatis eleifend. Donec commodo purus non mauris ultricies ultricies. Nulla facilisi.</p></div></div><div class=\"clear section\"><h3>Aenean?</h3><p>Aenean at urna nibh. Aliquam euismod tortor ut mauris eleifend ut vehicula neque convallis. Aenean dui orci, luctus non aliquet eu, semper non arcu. Aliquam tincidunt metus at ligula fringilla ornare. Praesent euismod, lacus vel condimentum convallis, massa quam auctor nisl, ut egestas felis sapien eget augue. Etiam eleifend auctor nunc, id facilisis ante ultrices in. Integer sagittis augue a tortor luctus ut tristique metus sagittis.</p></div><p>new paragraph</p>";
            t.Title = Resource.Template3ColumnsOver1ColumnTitle;
            t.ImageFileName = "columns3over1.gif";
            templates.Add(t);

            t = ContentTemplate.GetEmpty();
            //4 columns over 1
            t.Guid = new Guid("28ae8c68-b619-4e23-8dde-17d0a34ee7c6");
            t.Body = "<div class=\"floatpanel\"><div class=\"floatpanel section\" style=\"width: 23%;\"><h3>Lorem Ipsum</h3><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent varius varius est, id dictum lectus aliquet non. Fusce laoreet auctor facilisis. Nullam eget tortor at leo pellentesque pellentesque. Nunc tortor neque, elementum varius pretium sit amet, vulputate at erat. Duis nec nisi mauris, in gravida sapien.</p></div><div class=\"floatpanel section\" style=\"width: 23%;\"><h3>Duis a Mauris</h3><p>Duis a mauris non felis dapibus cursus. Aliquam eu dignissim purus. Donec at orci vitae sem laoreet molestie sed eu urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus magna velit, fringilla egestas vehicula at, adipiscing eget augue. Suspendisse porta, tellus id consequat volutpat.</p></div><div class=\"floatpanel section\" style=\"width: 23%;\"><h3>Vivamus Tristique!</h3><p>Vivamus tristique purus eget nisl sollicitudin varius. Praesent turpis sapien, imperdiet ut vehicula pretium, tristique nec mauris. Quisque eget lacus mi. Quisque adipiscing velit euismod enim venenatis eleifend. Donec commodo purus non mauris ultricies ultricies. Nulla facilisi.</p></div><div class=\"floatpanel section\" style=\"width: 23%;\"><h3>Sed Varius</h3><p>Sed varius porta consequat. Proin ante neque, mattis sit amet condimentum in, vulputate ac ipsum. Proin eu consequat est. Integer at vehicula lacus. Nulla faucibus dolor ut augue euismod eget volutpat ligula venenatis. Curabitur bibendum consequat orci, sagittis elementum dolor commodo vel.</p></div></div><div class=\"clear section\"><h3>Aenean?</h3><p>Aenean at urna nibh. Aliquam euismod tortor ut mauris eleifend ut vehicula neque convallis. Aenean dui orci, luctus non aliquet eu, semper non arcu. Aliquam tincidunt metus at ligula fringilla ornare. Praesent euismod, lacus vel condimentum convallis, massa quam auctor nisl, ut egestas felis sapien eget augue. Etiam eleifend auctor nunc, id facilisis ante ultrices in. Integer sagittis augue a tortor luctus ut tristique metus sagittis.</p></div><p>new paragraph</p>";
            t.Title = Resource.Template4ColumnsOver1ColumnTitle;
            t.ImageFileName = "columns4over1.gif";
            templates.Add(t);


            return templates;
        }


       
        public static FileInfo[] GetLogoList(SiteSettings siteSettings)
        {
            if (siteSettings == null) return null;

            string logoPath = HttpContext.Current.Server.MapPath
                (WebUtils.GetApplicationRoot() + "/Data/Sites/" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/logos/");

            DirectoryInfo dir = new DirectoryInfo(logoPath);
            return dir.Exists ? dir.GetFiles() : null;
        }

        public static FileInfo[] GetContentTemplateImageList(SiteSettings siteSettings)
        {
            if (siteSettings == null) return null;

            string filePath = HttpContext.Current.Server.MapPath
                (WebUtils.GetApplicationRoot() + "/Data/Sites/" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/htmltemplateimages/");

            DirectoryInfo dir = new DirectoryInfo(filePath);
            return dir.Exists ? dir.GetFiles() : null;
        }

        public static mojoPortal.Web.UI.Gravatar.RatingType GetMaxAllowedGravatarRating()
        {
            switch (WebConfigSettings.GravatarMaxAllowedRating)
            {
                case "PG":
                    return mojoPortal.Web.UI.Gravatar.RatingType.PG;

                case "R":
                    return mojoPortal.Web.UI.Gravatar.RatingType.R;

                case "X":
                    return mojoPortal.Web.UI.Gravatar.RatingType.X;


            }

            return mojoPortal.Web.UI.Gravatar.RatingType.G;
        }

        


        public static FileInfo[] GetAvatarList(SiteSettings siteSettings)
        {
            if (siteSettings == null) return null;

            string p = WebUtils.GetApplicationRoot() + "/Data/Sites/" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/avatars";
            string avatarPath = HttpContext.Current.Server.MapPath(p);

            DirectoryInfo dir = new DirectoryInfo(avatarPath);
            return dir.Exists ? dir.GetFiles("*.gif") : null;
        }


        public static FileInfo[] GetFeatureIconList()
        {
            string p = WebUtils.GetApplicationRoot() + "/Data/SiteImages/FeatureIcons";
            string filePath = HttpContext.Current.Server.MapPath(p);

            //HttpContext.Current.Request.PhysicalApplicationPath;
                

            DirectoryInfo dir = new DirectoryInfo(filePath);
            return dir.Exists ? dir.GetFiles("*.*") : null;
        }


        public static FileInfo[] GetFileIconList()
        {
            string p = WebUtils.GetApplicationRoot() + "/Data/SiteImages/Icons";
            string filePath = HttpContext.Current.Server.MapPath(p);

            DirectoryInfo dir = new DirectoryInfo(filePath);
            return dir.Exists ? dir.GetFiles("*.png") : null;
        }

        /// <summary>
        /// deprecated, better to just call IOHelper.GetMimeType
        /// </summary>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        public static string GetMimeType(string fileExtension)
        {
            return IOHelper.GetMimeType(fileExtension);
        }

        /// <summary>
        /// deprecated, better to just call IOHelper.IsNonAttacmentFileType
        /// </summary>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        public static bool IsNonAttacmentFileType(string fileExtension)
        {
            return IOHelper.IsNonAttacmentFileType(fileExtension);
        }

        public static string GetSiteSystemFolder()
        {
            if (HttpContext.Current == null) { return string.Empty; }
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return string.Empty; }

            return HttpContext.Current.Server.MapPath("~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/systemfiles/");


        }

        public static string GetSiteSkinFolderPath()
        {
            if (HttpContext.Current == null) { return string.Empty; }
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return string.Empty; }

            return HttpContext.Current.Server.MapPath("~/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/");


        }

        public static DirectoryInfo[] GetSkinList(SiteSettings siteSettings)
        {
            if (siteSettings == null) return null;

            string skinPath = HttpContext.Current.Server.MapPath
                (WebUtils.GetApplicationRoot() + "/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/");

            DirectoryInfo dir = new DirectoryInfo(skinPath);
            return dir.Exists ? dir.GetDirectories() : null;
        }

        public static DirectoryInfo[] GetSkinCatalogList()
        {
            string skinPath = HttpContext.Current.Server.MapPath ("~/Data/skins/");

            DirectoryInfo dir = new DirectoryInfo(skinPath);
            return dir.Exists ? dir.GetDirectories() : null;
        }

        public static FileInfo[] GetCodeTemplateList()
        {
            string filePath = HttpContext.Current.Server.MapPath("~/DevAdmin/CodeTemplates");

            DirectoryInfo dir = new DirectoryInfo(filePath);
            return dir.Exists ? dir.GetFiles("*.aspx") : null;
        }


        public static SmtpSettings GetSmtpSettings()
        {
            SmtpSettings smtpSettings = new SmtpSettings();

            if (WebConfigSettings.EnableSiteSettingsSmtpSettings)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if (siteSettings != null)
                {
                    if (WebConfigSettings.UseLegacyCryptoHelper)
                    {
                        if (siteSettings.SMTPUser.Length > 0)
                        {
                            smtpSettings.User = CryptoHelper.Decrypt(siteSettings.SMTPUser);
                        }

                        if (siteSettings.SMTPPassword.Length > 0)
                        {
                            smtpSettings.Password = CryptoHelper.Decrypt(siteSettings.SMTPPassword);
                        }
                    }
                    else
                    {
                        if (siteSettings.SMTPUser.Length > 0)
                        {
                            smtpSettings.User = SiteUtils.Decrypt(siteSettings.SMTPUser);
                        }

                        if (siteSettings.SMTPPassword.Length > 0)
                        {
                            smtpSettings.Password = SiteUtils.Decrypt(siteSettings.SMTPPassword);
                        }
                    }

                    smtpSettings.Server = siteSettings.SMTPServer;
                    smtpSettings.Port = siteSettings.SMTPPort;
                    smtpSettings.RequiresAuthentication = siteSettings.SMTPRequiresAuthentication;
                    smtpSettings.UseSsl = siteSettings.SMTPUseSsl;
                    smtpSettings.PreferredEncoding = siteSettings.SMTPPreferredEncoding;


                }

            }
            else
            {
                if (ConfigurationManager.AppSettings["SMTPUser"] != null)
                {
                    smtpSettings.User = ConfigurationManager.AppSettings["SMTPUser"];
                }

                if (ConfigurationManager.AppSettings["SMTPPassword"] != null)
                {
                    smtpSettings.Password = ConfigurationManager.AppSettings["SMTPPassword"];
                }
                if (ConfigurationManager.AppSettings["SMTPServer"] != null)
                {
                    smtpSettings.Server = ConfigurationManager.AppSettings["SMTPServer"];
                }

                smtpSettings.Port = ConfigHelper.GetIntProperty("SMTPPort", 25);

                bool byPassContext = true;
                smtpSettings.RequiresAuthentication = ConfigHelper.GetBoolProperty("SMTPRequiresAuthentication", false, byPassContext); ;
                smtpSettings.UseSsl = ConfigHelper.GetBoolProperty("SMTPUseSsl", false, byPassContext);

                if (
               (ConfigurationManager.AppSettings["SmtpPreferredEncoding"] != null)
               && (ConfigurationManager.AppSettings["SmtpPreferredEncoding"].Length > 0)
               )
                {
                    smtpSettings.PreferredEncoding = ConfigurationManager.AppSettings["SmtpPreferredEncoding"];
                }

            }

            return smtpSettings;
        }

        

        /// <summary>
        /// deprecated, you should pass in the Page
        /// </summary>
        /// <returns></returns>
        public static string GetSkinBaseUrl()
        {
            return GetSkinBaseUrl(null);
        }

        public static string GetSkinBaseUrl(Page page)
        {
            bool allowPageOverride = true;
            return GetSkinBaseUrl(allowPageOverride, page);
        }


        public static string GetSkinBaseUrl(bool allowPageOverride, Page page)
        {
            if (allowPageOverride) return GetSkinBaseUrlWithOverride(page);

            return GetSkinBaseUrlNoOverride(page);
        }

        private static string GetSkinBaseUrlNoOverride(Page page)
        {
            if (HttpContext.Current == null) return String.Empty;

            string baseUrl = HttpContext.Current.Items["skinBaseUrlfalse"] as string;
            if (baseUrl == null)
            {
                baseUrl = DetermineSkinBaseUrl(false, page);
                if (baseUrl != null)
                    HttpContext.Current.Items["skinBaseUrlfalse"] = baseUrl;
            }
            return baseUrl;


        }

        private static string GetSkinBaseUrlWithOverride(Page page)
        {
            if (HttpContext.Current == null) return String.Empty;

            string baseUrl = HttpContext.Current.Items["skinBaseUrltrue"] as string;
            if (baseUrl == null)
            {
                baseUrl = DetermineSkinBaseUrl(true, page);
                if (baseUrl != null)
                    HttpContext.Current.Items["skinBaseUrltrue"] = baseUrl;
            }
            return baseUrl;


        }

        private static string DetermineSkinBaseUrl(bool allowPageOverride, Page page)
        {
            bool fullUrl = true;

            return DetermineSkinBaseUrl(allowPageOverride, fullUrl, page);

            
        }

        public static string DetermineSkinBaseUrl(string skinName)
        {
            if(string.IsNullOrEmpty(skinName)) { return WebUtils.GetSiteRoot() + "/Data/Skins/styleshout-refresh/"; }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return WebUtils.GetSiteRoot() + "/Data/Skins/styleshout-refresh/"; }
            

            string skinUrl = WebUtils.GetSiteRoot() + "/Data/Sites/"
                    + siteSettings.SiteId.ToInvariantString()
                    + "/skins/" + skinName + "/";

            return skinUrl;

        }

        public static string DetermineSkinBaseUrl(bool allowPageOverride, bool fullUrl, Page page)
        {
            string skinFolder;
            string siteRoot = string.Empty;

            if (fullUrl)
            {
                siteRoot = WebUtils.GetSiteRoot();
                skinFolder = siteRoot + "/Data/Sites/1/skins/";
            }
            else
            {
                siteRoot = WebUtils.GetRelativeSiteRoot();
                skinFolder = "/Data/Sites/1/skins/";
            }

            string currentSkin = "styleshout-refresh/";

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            PageSettings currentPage = CacheHelper.GetCurrentPage();

            if (siteSettings != null)
            {
                currentSkin = siteSettings.Skin + "/";

                if (siteSettings.AllowUserSkins)
                {
                    string skinCookieName = "mojoUserSkin" + siteSettings.SiteId.ToInvariantString();
                    if (CookieHelper.CookieExists(skinCookieName))
                    {
                        string cookieValue = CookieHelper.GetCookieValue(skinCookieName);
                        if (cookieValue.Length > 0)
                        {
                            currentSkin = cookieValue + "/";
                        }
                    }
                }

                if (
                    (allowPageOverride)
                    && (currentPage != null)
                    && (siteSettings.AllowPageSkins)
                    && ((page != null)
                        && (
                        (page is mojoPortal.Web.AdminUI.PageLayout)
                        || (page is mojoPortal.Web.AdminUI.PageProperties)
                        ||(!(page is NonCmsBasePage))
                        )
                       )
                    )
                {
                    if (currentPage.Skin.Length > 0)
                    {
                        currentSkin = currentPage.Skin + "/";

                    }
                }

                skinFolder = siteRoot + "/Data/Sites/"
                    + siteSettings.SiteId.ToInvariantString() + "/skins/";


                if (HttpContext.Current.Request.Params.Get("skin") != null)
                {
                    currentSkin = SanitizeSkinParam(HttpContext.Current.Request.Params.Get("skin")) + "/";
                }
            }

            return skinFolder + currentSkin;
        }

        public static string GetEditorStyleSheetUrl(bool allowPageOverride, bool fullUrl, Page page)
        {
            if (WebConfigSettings.EditorCssUrlOverride.Length > 0) { return WebConfigSettings.EditorCssUrlOverride; }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) return string.Empty;

            if (page != null) 
            {
                if (fullUrl)
                {
                    return WebUtils.ResolveServerUrl("~/csshandler.ashx?skin=") + SiteUtils.GetSkinName(allowPageOverride, page) + "&amp;s=" + siteSettings.SiteId.ToInvariantString();
                }
                else
                {
                    return page.ResolveUrl("~/csshandler.ashx?skin=") + SiteUtils.GetSkinName(allowPageOverride, page) + "&amp;s=" + siteSettings.SiteId.ToInvariantString();
                }
                
            }

            string editorCss = "csshandler.ashx";
            if (WebConfigSettings.UsingOlderSkins)
            {
                editorCss = "style.css";
            }

            string skinName = "styleshout-techmania";

            

            string basePath = WebUtils.GetSiteRoot() + "/Data/Sites/" + siteSettings.SiteId.ToInvariantString() + "/skins/";

            //SiteSettings siteSettings = (SiteSettings) HttpContext.Current.Items["SiteSettings"];

            PageSettings currentPage = CacheHelper.GetCurrentPage();
            if (siteSettings != null)
            {
                // very old skins were .ascx
                skinName = siteSettings.Skin.Replace(".ascx", string.Empty);

                if (siteSettings.AllowUserSkins)
                {
                    string skinCookieName = "mojoUserSkin" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture);

                    if (CookieHelper.CookieExists(skinCookieName))
                    {
                        string cookieValue = CookieHelper.GetCookieValue(skinCookieName);
                        if (cookieValue.Length > 0)
                        {
                            skinName = cookieValue.Replace(".ascx", string.Empty);
                        }
                    }
                }

                if ((currentPage != null) && (siteSettings.AllowPageSkins))
                {
                    if (currentPage.Skin.Length > 0)
                    {
                        skinName = currentPage.Skin.Replace(".ascx", string.Empty);

                    }
                }



                if (HttpContext.Current.Request.Params.Get("skin") != null)
                {
                    skinName = HttpContext.Current.Request.Params.Get("skin");

                }

                if (editorCss == "csshandler.ashx")
                {
                    return basePath + skinName + "/" + editorCss + "?skin=" + skinName;
                }

                return basePath + skinName + "/" + editorCss;

            }

            return "/Data/Sites/1/skins/styleshout-tecmania/style.css";
        }

        public static void SetupEditor(EditorControl editor)
        {
            SetupEditor(editor, WebConfigSettings.UseSkinCssInEditor);
        }

        /// <summary>
        /// this is the preferred overload
        /// </summary>
        /// <param name="editor"></param>
        /// <param name="allowPageOverride"></param>
        /// <param name="fullUrl"></param>
        /// <param name="page"></param>
        public static void SetupEditor(EditorControl editor, bool allowPageOverride, Page page)
        {
            SetupEditor(editor, WebConfigSettings.UseSkinCssInEditor, string.Empty, allowPageOverride, false, page);
        }

        public static void SetupEditor(EditorControl editor, bool useSkinCss)
        {
            SetupEditor(editor, useSkinCss, string.Empty, false, true, null);
        }

        /// <summary>
        /// You should pass your editor to this method during pre-init or init
        /// </summary>
        /// <param name="editor"></param>
        public static void SetupEditor(EditorControl editor, bool useSkinCss, string preferredProvider, bool allowPageOverride, bool fullUrl, Page page)
        {
            if (HttpContext.Current == null) return;
            if (HttpContext.Current.Request == null) return;
            if (editor == null) return;

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) return;

            string providerName = siteSettings.EditorProviderName;
            if (siteSettings.AllowUserEditorPreference)
            {
                SiteUser siteUser = GetCurrentSiteUser();
                if ((siteUser != null) && (siteUser.EditorPreference.Length > 0))
                {
                    providerName = siteUser.EditorPreference;
                }

            }


            string loweredBrowser = string.Empty;

            if (HttpContext.Current.Request.UserAgent != null)
            {
                loweredBrowser = HttpContext.Current.Request.UserAgent.ToLower();
            }


            if (
                (loweredBrowser.Contains("safari"))
                && (WebConfigSettings.ForceTinyMCEInSafari)
                )
            {
                providerName = "TinyMCEProvider";
            }

            if (
                (loweredBrowser.Contains("opera"))
                && (WebConfigSettings.ForceTinyMCEInOpera)
                )
            {
                providerName = "TinyMCEProvider";
            }

            if (
                (loweredBrowser.Contains("iphone"))
                && (WebConfigSettings.ForcePlainTextInIphone)
                )
            {
                providerName = "TextAreaProvider";
            }

            if (
                (loweredBrowser.Contains("ipad"))
                && (WebConfigSettings.ForcePlainTextInIpad)
                )
            {
                providerName = "TextAreaProvider";
            }

            if (
                (loweredBrowser.Contains("android"))
                && (WebConfigSettings.ForcePlainTextInAndroid)
                )
            {
                providerName = "TextAreaProvider";
            }

            string siteRoot = null;
            if (siteSettings.SiteFolderName.Length > 0)
            {
                siteRoot = siteSettings.SiteRoot;
            }
            if (siteRoot == null) siteRoot = WebUtils.GetSiteRoot();

            if (!string.IsNullOrEmpty(preferredProvider)) { providerName = preferredProvider; }

            editor.ProviderName = providerName;
            if (editor.WebEditor != null)
            {
                editor.WebEditor.SiteRoot = siteRoot;
                //editor.WebEditor.SkinName = siteSettings.EditorSkin.ToString();
                if (useSkinCss)
                {
                    editor.WebEditor.EditorCSSUrl = GetEditorStyleSheetUrl(allowPageOverride, fullUrl, page);
                }

                CultureInfo defaultCulture = SiteUtils.GetDefaultCulture();
                if (defaultCulture.TextInfo.IsRightToLeft)
                {
                    editor.WebEditor.TextDirection = Direction.RightToLeft;
                }
            }



        }

        public static int ParseSiteIdFromSkinRequestUrl()
        {
            int siteId = -1;

            if (HttpContext.Current == null) { return siteId; }
            if (HttpContext.Current.Request == null) { return siteId; }

            if (
                (HttpContext.Current.Request.RawUrl.IndexOf("Data/Sites/") == -1)
                || (HttpContext.Current.Request.RawUrl.IndexOf("/skins/") == -1)
                )
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if (siteSettings != null) { return siteSettings.SiteId; }

            }

            string tagged = HttpContext.Current.Request.RawUrl.Replace("/Sites/", "|").Replace("/skins/", "|");
            try
            {
                string strId = tagged.Substring(tagged.IndexOf("|") + 1, tagged.LastIndexOf("|") - tagged.IndexOf("|") - 1);

                //log.Info("Parsing siteId to " + strId);

                int.TryParse(strId, NumberStyles.Integer, CultureInfo.InvariantCulture, out siteId);
            }
            catch (ArgumentOutOfRangeException)
            {
                log.Error("Could not parse siteid from skin url so using SiteSettings.");
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if (siteSettings != null) { return siteSettings.SiteId; }
            
            }

            return siteId;


        }

        //public static string GetCssHandlerBaseUrl(string skinName)
        //{
        //    if (string.IsNullOrEmpty(skinName)) { return GetCssHandlerBaseUrl(false); }

        //    SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
        //    if (siteSettings == null) { return GetNavigationSiteRoot() + "/Data/Skins/styleshout-refresh/"; }


        //    string skinUrl = GetNavigationSiteRoot() + "/Data/Sites/"
        //            + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture)
        //            + "/skins/" + skinName + "/";

        //    return skinUrl;

        //}

        //public static string GetCssHandlerBaseUrl(bool allowPageOverride)
        //{
        //    string skinFolder;
        //    string siteRoot = string.Empty;


        //    siteRoot = GetNavigationSiteRoot();
        //    skinFolder = siteRoot + "/Data/Sites/1/skins/";
            
        //    string currentSkin = "styleshout-refresh/";

        //    SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
        //    PageSettings currentPage = CacheHelper.GetCurrentPage();

        //    if (siteSettings != null)
        //    {
        //        currentSkin = siteSettings.Skin + "/";

        //        if (siteSettings.AllowUserSkins)
        //        {
        //            string skinCookieName = "mojoUserSkin" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture);
        //            if (CookieHelper.CookieExists(skinCookieName))
        //            {
        //                string cookieValue = CookieHelper.GetCookieValue(skinCookieName);
        //                if (cookieValue.Length > 0)
        //                {
        //                    currentSkin = cookieValue + "/";
        //                }
        //            }
        //        }

        //        if (
        //            (allowPageOverride)
        //            && (currentPage != null)
        //            && (siteSettings.AllowPageSkins)
        //            && (IsContentPage())
        //            )
        //        {
        //            if (currentPage.Skin.Length > 0)
        //            {
        //                currentSkin = currentPage.Skin + "/";

        //            }
        //        }

        //        skinFolder = siteRoot + "/Data/Sites/"
        //            + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/skins/";


        //        if (HttpContext.Current.Request.Params.Get("skin") != null)
        //        {
        //            currentSkin = SanitizeSkinParam(HttpContext.Current.Request.Params.Get("skin")) + "/";
        //        }
        //    }

        //    return skinFolder + currentSkin;
        //}

        public static string GetSkinName(bool allowPageOverride, Page page)
        {
            
            string currentSkin = "styleshout-refresh";

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            PageSettings currentPage = CacheHelper.GetCurrentPage();

            if (siteSettings != null)
            {
                currentSkin = siteSettings.Skin;

                if (siteSettings.AllowUserSkins)
                {
                    string skinCookieName = "mojoUserSkin" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture);
                    if (CookieHelper.CookieExists(skinCookieName))
                    {
                        string cookieValue = CookieHelper.GetCookieValue(skinCookieName);
                        if (cookieValue.Length > 0)
                        {
                            currentSkin = cookieValue;
                        }
                    }
                }

                if (
                    (allowPageOverride)
                    && (currentPage != null)
                    && (siteSettings.AllowPageSkins)
                   
                    )
                {
                    if (currentPage.Skin.Length > 0)
                    {
                        currentSkin = currentPage.Skin;

                    }
                }

                
                if (HttpContext.Current.Request.Params.Get("skin") != null)
                {
                    currentSkin = SanitizeSkinParam(HttpContext.Current.Request.Params.Get("skin"));
                }

            }

            return currentSkin;
        }

        //public static string GetStyleSheetLinks(bool allowPageOverride)
        //{
        //    string styleLinks;
        //    string baseSkinUrl = SiteUtils.GetSkinBaseUrl(allowPageOverride);

        //    if (WebConfigSettings.IncludeTextSizeCss)
        //    {
        //        styleLinks
        //            = "\n<link href='" + baseSkinUrl
        //            + "style.css' type='text/css' rel='stylesheet' />"
        //            + "\n<link href='" + baseSkinUrl
        //            + "styletext.css' type='text/css' rel='stylesheet' title='"
        //            + Resource.NormalTextLabel + "' />"
        //            + "\n<link href='" + baseSkinUrl
        //            + "styletextmedium.css' type='text/css' rel='alternate stylesheet' title='"
        //            + Resource.MediumTextLabel + "' />"
        //            + "\n<link href='" + baseSkinUrl
        //            + "styletextlarge.css' type='text/css' rel='alternate stylesheet' title='"
        //            + Resource.LargeTextLabel + "' />"
        //            + "\n<link href='" + baseSkinUrl
        //            + "styleprinter.css' type='text/css' rel='stylesheet' media='print' />";
        //    }
        //    else
        //    {
        //        styleLinks
        //            = "\n<link href='" + baseSkinUrl
        //            + "style.css' type='text/css' rel='stylesheet' />"
        //            + "\n<link href='" + baseSkinUrl
        //            + "styletext.css' type='text/css' rel='stylesheet' title='"
        //            + Resource.NormalTextLabel + "' />"
        //            + "\n<link href='" + baseSkinUrl
        //            + "styleprinter.css' type='text/css' rel='stylesheet' media='print' />";
        //    }

        //    return styleLinks;

        //}


        public static string GetStyleSheetUrl(Page page)
        {
            bool allowPageOverride = false;
            string cssUrl = SiteUtils.GetSkinBaseUrl(allowPageOverride, page)
            + "csshandler.ashx?skin=" + SiteUtils.GetSkinName(allowPageOverride, page)
                    + "&amp;config=style.config";

            return cssUrl;
        }

        public static string ChangeRelativeUrlsToFullyQuailifiedUrls(string navigationSiteRoot, string imageSiteRoot, string htmlContent)
        {
            if (string.IsNullOrEmpty(htmlContent)) { return htmlContent; }
            if (string.IsNullOrEmpty(navigationSiteRoot)) { return htmlContent; }
            if (string.IsNullOrEmpty(imageSiteRoot)) { return htmlContent; }

            return htmlContent.Replace("href=\"/", "href=\"" + navigationSiteRoot + "/").Replace("href='/", "href='" + navigationSiteRoot + "/").Replace("src=\"/", "src=\"" + imageSiteRoot + "/").Replace("src='/", "src='" + imageSiteRoot + "/");

        }

        //public static string GetRegexRelativeImageUrlPatern()
        //{
        //    return "^" + WebUtils.GetSiteRoot() + @"/.*[_a-zA-Z0-9]+\.(png|jpg|jpeg|gif|PNG|JPG|JPEG|GIF)$";
        //}
        

        public static string GetNavigationSiteRoot()
        {
            if (HttpContext.Current == null) return string.Empty;

            if (HttpContext.Current.Items["navigationRoot"] != null)
            {
                return HttpContext.Current.Items["navigationRoot"].ToString();
            }
            
            string navigationRoot = WebUtils.GetSiteRoot();
            bool useFolderForSiteDetection = ConfigHelper.GetBoolProperty("UseFoldersInsteadOfHostnamesForMultipleSites", false);

            if (useFolderForSiteDetection)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

                if ((siteSettings != null)
                    && (siteSettings.SiteFolderName.Length > 0))
                {
                    navigationRoot = siteSettings.SiteRoot;
                }
            }

            HttpContext.Current.Items["navigationRoot"] = navigationRoot;
            
            return navigationRoot;
           
        }

        public static string GetRelativeNavigationSiteRoot()
        {
            if (HttpContext.Current == null) return string.Empty;

            if (HttpContext.Current.Items["relativenavigationRoot"] != null)
            {
                return HttpContext.Current.Items["relativenavigationRoot"].ToString();
            }

            string navigationRoot = WebUtils.GetRelativeSiteRoot();
            bool useFolderForSiteDetection = ConfigHelper.GetBoolProperty("UseFoldersInsteadOfHostnamesForMultipleSites", false);

            if (useFolderForSiteDetection)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

                if ((siteSettings != null)
                    && (siteSettings.SiteFolderName.Length > 0))
                {
                    navigationRoot = siteSettings.SiteRoot;
                }
            }

            HttpContext.Current.Items["relativenavigationRoot"] = navigationRoot;

            return navigationRoot;

        }

        //public static string GetInsecureNavigationSiteRoot()
        //{
        //    return GetNavigationSiteRoot().Replace("https", "http");
        //}

        public static string GetSecureNavigationSiteRoot()
        {
            if (HttpContext.Current == null) return string.Empty;

            if (HttpContext.Current.Items["securenavigationRoot"] != null)
            {
                return HttpContext.Current.Items["securenavigationRoot"].ToString();
            }

            string navigationRoot = WebUtils.GetSecureSiteRoot();
            bool useFolderForSiteDetection = ConfigHelper.GetBoolProperty("UseFoldersInsteadOfHostnamesForMultipleSites", false);

            if (useFolderForSiteDetection)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

                if ((siteSettings != null)
                    && (siteSettings.SiteFolderName.Length > 0))
                {
                    navigationRoot = navigationRoot + "/" + siteSettings.SiteFolderName;
                }
            }

            HttpContext.Current.Items["securenavigationRoot"] = navigationRoot;

            return navigationRoot;

        }

        public static string GetInSecureNavigationSiteRoot()
        {
            if (HttpContext.Current == null) return string.Empty;

            if (HttpContext.Current.Items["securenavigationRoot"] != null)
            {
                return HttpContext.Current.Items["securenavigationRoot"].ToString();
            }

            string navigationRoot = WebUtils.GetInSecureSiteRoot();
            bool useFolderForSiteDetection = ConfigHelper.GetBoolProperty("UseFoldersInsteadOfHostnamesForMultipleSites", false);

            if (useFolderForSiteDetection)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

                if ((siteSettings != null)
                    && (siteSettings.SiteFolderName.Length > 0))
                {
                    navigationRoot = navigationRoot + "/" + siteSettings.SiteFolderName;
                }
            }

            HttpContext.Current.Items["securenavigationRoot"] = navigationRoot;

            return navigationRoot;

        }

        public static string GetCurrentPageUrl()
        {
            PageSettings currentPage = CacheHelper.GetCurrentPage();
            return GetPageUrl(currentPage);

        }

        private static string PageTitleFormatName()
        {
            if (HttpContext.Current == null) { return string.Empty; }

            if (HttpContext.Current.Items["PageTitleFormatName"] != null)
            {
                return HttpContext.Current.Items["PageTitleFormatName"].ToString();
            }

            HttpContext.Current.Items["PageTitleFormatName"] = WebConfigSettings.PageTitleFormatName;

            return HttpContext.Current.Items["PageTitleFormatName"].ToString();

        }

        private static string PageTitleSeparatorString()
        {
            if (HttpContext.Current == null) { return string.Empty; }

            if (HttpContext.Current.Items["PageTitleSeparatorString"] != null)
            {
                return HttpContext.Current.Items["PageTitleSeparatorString"].ToString();
            }

            HttpContext.Current.Items["PageTitleSeparatorString"] = WebConfigSettings.PageTitleSeparatorString;

            return HttpContext.Current.Items["PageTitleSeparatorString"].ToString();

        }

        public const string TitleFormat_TitleOnly = "TitleOnly";
        public const string TitleFormat_SitePlusTitle = "SitePlusTitle";
        public const string TitleFormat_TitlePlusSite = "TitlePlusSite";

        public static string FormatPageTitle(SiteSettings siteSettings, string topicTitle)
        {
            if (siteSettings == null) { return topicTitle; }

            string pageTitle;
            switch (PageTitleFormatName())
            {
                case TitleFormat_TitleOnly:
                    pageTitle = topicTitle;
                    break;
                case TitleFormat_TitlePlusSite:
                    pageTitle = topicTitle + PageTitleSeparatorString() + siteSettings.SiteName;
                    break;
                case TitleFormat_SitePlusTitle:
                default:
                    pageTitle = siteSettings.SiteName + PageTitleSeparatorString() + topicTitle;
                    break;
            }

            if ((pageTitle.Length > 65) && (WebConfigSettings.AutoTruncatePageTitles))
            {
                pageTitle = UIHelper.CreateExcerpt(pageTitle, 65);
            }

            return pageTitle;

        }

        public static string GetPageUrl(PageSettings pageSettings)
        {
            string navigationRoot = string.Empty;
            bool useFolderForSiteDetection = ConfigHelper.GetBoolProperty("UseFoldersInsteadOfHostnamesForMultipleSites", false);

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (
                (siteSettings == null)
                ||(pageSettings == null)
                ||(pageSettings.PageId == -1)
                ||(pageSettings.SiteId != siteSettings.SiteId)
                )
            {
                navigationRoot = WebUtils.GetSiteRoot();
                return navigationRoot;
            }

            if (pageSettings.Url.StartsWith("http")) return pageSettings.Url;

            string resolvedUrl;

            if (pageSettings.UseUrl)
            {
                
                //if ((pageSettings.Url.StartsWith("~/")) && (pageSettings.Url.EndsWith(".aspx")))
                if (pageSettings.Url.StartsWith("~/")) 
                {
                    if (pageSettings.UrlHasBeenAdjustedForFolderSites)
                    {
                        resolvedUrl = pageSettings.Url.Replace("~/", "/");
                    }
                    else
                    {
                        resolvedUrl = siteSettings.SiteRoot
                            + pageSettings.Url.Replace("~/", "/");
                    }

                }
                else
                {
                    resolvedUrl = pageSettings.Url;
                }

            }
            else
            {
                resolvedUrl = siteSettings.SiteRoot
                    + "/Default.aspx?pageid="
                    + pageSettings.PageId.ToInvariantString();
                
            }



            return resolvedUrl;

        }

        public static string GetFileAttachmentUploadPath()
        {
            if (HttpContext.Current == null) { return string.Empty; }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

            if (siteSettings == null) { return string.Empty; }

            return HttpContext.Current.Server.MapPath(WebUtils.GetApplicationRoot() + "/Data/Sites/"
                + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/Attachments/");

        }

        public static void EnsureFileAttachmentFolder(SiteSettings siteSettings)
        {
            if (siteSettings == null) { return; }
            if (HttpContext.Current == null) { return; }

            string filePath = HttpContext.Current.Server.MapPath(WebUtils.GetApplicationRoot() + "/Data/Sites/"
                + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/Attachments/");

            if (!Directory.Exists(filePath))
            {
                try
                {
                    Directory.CreateDirectory(filePath);
                }
                catch (IOException ex)
                {
                    log.Error("failed to create path for file attachments " + filePath + " ", ex);
                }
            }

        }

        /// <summary>
        /// encapsulates checks for a secure connection with configurable server variable checks
        /// </summary>
        /// <returns></returns>
        public static bool IsSecureRequest()
        {
            if((HttpContext.Current != null)&&(HttpContext.Current.Request != null))
            {
                // default this works when the SSL certificate is installed in the site but not when using load balancers or other proxy server
                if (HttpContext.Current.Request.IsSecureConnection) { return true; } 

                if (WebConfigSettings.SecureConnectionServerVariableForPresenceCheck.Length > 0)
                {
                    if (HttpContext.Current.Request.ServerVariables[WebConfigSettings.SecureConnectionServerVariableForPresenceCheck] != null) { return true; }
                }

                if ((WebConfigSettings.SecureConnectionServerVariableForValueCheck.Length > 0) && (WebConfigSettings.SecureConnectionServerVariableSecureValue.Length > 0))
                {
                    if (HttpContext.Current.Request.ServerVariables[WebConfigSettings.SecureConnectionServerVariableForValueCheck] != null)
                    {
                        if (HttpContext.Current.Request.ServerVariables[WebConfigSettings.SecureConnectionServerVariableForValueCheck] == WebConfigSettings.SecureConnectionServerVariableSecureValue) { return true; }
                    }
                }

            }
            

            return false;
        }

        public static bool SslIsAvailable()
        {
            if (WebConfigSettings.SslisAvailable) { return true; }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return false; }
            string key = "Site" + siteSettings.SiteId.ToInvariantString() + "-SSLIsAvailable";
            if (ConfigurationManager.AppSettings[key] != null)
            {
                return ConfigHelper.GetBoolProperty(key, false);
            }
           
            return false;

        }

        //public static void ForceSsl()
        //{
        //    if (WebConfigSettings.UseFoldersInsteadOfHostnamesForMultipleSites)
        //    {
                
        //        if (!WebConfigSettings.ProxyPreventsSSLDetection)
        //        {
        //            string pageUrl = HttpContext.Current.Request.Url.ToString();
        //            if (pageUrl.StartsWith("http:"))
        //            {
        //                string secureUrl ;

        //                if (WebConfigSettings.IsRunningInRootSite)
        //                {
        //                    secureUrl = WebUtils.GetSecureSiteRoot()
        //                    + HttpContext.Current.Request.RawUrl;
        //                }
        //                else
        //                {
        //                    secureUrl = WebUtils.GetSecureHostRoot()
        //                    + HttpContext.Current.Request.RawUrl;
        //                }
                        
        //                HttpContext.Current.Response.Redirect(secureUrl, true);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        if (!WebConfigSettings.EnableSslInChildSites)
        //        {
        //            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
        //            if (!siteSettings.IsServerAdminSite) return;

        //        }
        //        WebUtils.ForceSsl();
        //    }
        //}

        public static void ForceSsl()
        {
            if (!WebConfigSettings.EnableSslInChildSites)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if (!siteSettings.IsServerAdminSite) return;

            }

            
            if (!WebConfigSettings.ProxyPreventsSSLDetection)
            {
                string pageUrl = HttpContext.Current.Request.Url.ToString();
                if (pageUrl.StartsWith("http:"))
                {
                    string secureUrl;

                    if (WebConfigSettings.IsRunningInRootSite)
                    {
                        secureUrl = WebUtils.GetSecureSiteRoot()
                        + HttpContext.Current.Request.RawUrl;
                    }
                    else
                    {
                        secureUrl = WebUtils.GetSecureHostRoot()
                        + HttpContext.Current.Request.RawUrl;
                    }

                    HttpContext.Current.Response.Redirect(secureUrl, true);
                }
            }
            
        }

        public static void ClearSsl()
        {
            if (HttpContext.Current == null) { return; }
            //if (WebConfigSettings.UseFoldersInsteadOfHostnamesForMultipleSites)
            //{
            string pageUrl = HttpContext.Current.Request.Url.ToString();
            if (pageUrl.StartsWith("https:"))
            {
                string insecureUrl;

                if (WebConfigSettings.IsRunningInRootSite)
                {
                    insecureUrl = WebUtils.GetInSecureSiteRoot()
                    + HttpContext.Current.Request.RawUrl;

                }
                else
                {
                    insecureUrl = WebUtils.GetInSecureHostRoot()
                    + HttpContext.Current.Request.RawUrl;
                }
                

                HttpContext.Current.Response.Redirect(insecureUrl, true);
            }
            //}
            //else
            //{
                
            //    WebUtils.ClearSsl();
            //}
        }

        /// <summary>
        /// this uses the membership provider to encrypt strings the same way that passwords are encrypted
        /// </summary>
        /// <param name="unencrypted"></param>
        /// <returns></returns>
        public static string Encrypt(string unencrypted)
        {
            mojoMembershipProvider m = Membership.Provider as mojoMembershipProvider;
            if (m == null) { throw new System.InvalidOperationException("could not obtain membership provider to use for encryption"); }

            return m.EncodePassword(unencrypted, MembershipPasswordFormat.Encrypted);

        }

        /// <summary>
        /// this uses th emembership provider to decrypt strings that were encrypted with the membership provider
        /// </summary>
        /// <param name="encrypted"></param>
        /// <returns></returns>
        public static string Decrypt(string encrypted)
        {
            mojoMembershipProvider m = Membership.Provider as mojoMembershipProvider;
            if (m == null) { throw new System.InvalidOperationException("could not obtain membership provider to use for decryption"); }

            return m.UnencodePassword(encrypted, MembershipPasswordFormat.Encrypted);

        }

        //http://msdn.microsoft.com/en-us/library/ff649308.aspx
        private static string GenerateKey(int length)
        {
            byte[] buff = new byte[length / 2];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(buff);
            StringBuilder key = new StringBuilder(length);
            for (int i = 0; i < buff.Length; i++)
                key.Append(string.Format("{0:X2}", buff[i]));

            return key.ToString();
        }

        public static string GenerateRandomMachineKey()
        {
            string validationKey = GenerateKey(128);
            string decryptionKey = GenerateKey(64);
            
            return "<machineKey validationKey=\"" + validationKey + "\" decryptionKey=\"" + decryptionKey + "\" validation=\"SHA1\" decryption=\"AES\" />";
        }

        

        public static string GetEditorProviderName()
        {
            string providerName = "FCKeditorProvider";

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings != null)
            {
                providerName = siteSettings.EditorProviderName;
            }


            if (HttpContext.Current != null)
            {
                string loweredBrowser = HttpContext.Current.Request.Browser.Browser.ToLower();
                // FCKeditor doesn't work in safari or opera
                // so just force TinyMCE
                if (
                    (loweredBrowser.Contains("safari"))
                    &&(WebConfigSettings.ForceTinyMCEInSafari)
                    )
                {
                    providerName = "TinyMCEProvider";
                }

                if (
                    (loweredBrowser.Contains("opera"))
                    && (WebConfigSettings.ForceTinyMCEInOpera)
                    )
                {
                    providerName = "TinyMCEProvider";
                }

            }

            //TODO: could have user preferences

            return providerName;
        }

        public static bool IsMobileDevice()
        {
            if (HttpContext.Current == null) { return false; }
            if (HttpContext.Current.Request.UserAgent == null) { return false; }

            string loweredBrowser = HttpContext.Current.Request.UserAgent.ToLower();

            List<string> mobileAgents = WebConfigSettings.MobilePhoneUserAgents.SplitOnCharAndTrim(',');
            foreach (string agent in mobileAgents)
            {
                if (loweredBrowser.Contains(agent)) { return true; }
            }


            return false;
            

        }

        public static void SetCurrentSkinBaseUrl(SiteSettings siteSettings)
        {
            string siteRoot = WebUtils.GetSiteRoot();
            string skinFolder = siteRoot + "/Data/Sites/1/skins/";
            string currentSkin = "jwv1/";


            if (siteSettings != null)
            {
                currentSkin = siteSettings.Skin + "/";

                if (siteSettings.AllowUserSkins)
                {
                    string skinCookieName = "mojoUserSkin" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture);

                    if (CookieHelper.CookieExists(skinCookieName))
                    {
                        string cookieValue = CookieHelper.GetCookieValue(skinCookieName);
                        if (cookieValue.Length > 0)
                        {
                            currentSkin = cookieValue + "/";
                        }
                    }
                }

                //if (siteSettings.AllowPageSkins)
                //{
                //    if (siteSettings.ActivePage.Skin.Length > 0)
                //    {
                //        currentSkin = siteSettings.ActivePage.Skin + "/";

                //    }
                //}

                skinFolder = siteRoot + "/Data/Sites/"
                    + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/skins/";


                //if (HttpContext.Current.Request.Params.Get("skin") != null)
                //{
                //    currentSkin = HttpContext.Current.Request.Params.Get("skin") + "/";

                //}

                siteSettings.SkinBaseUrl = skinFolder + currentSkin;
            }


        }


        #region Url Rewrite tracking

        public static void TrackUrlRewrite()
        {
            if (HttpContext.Current == null) return;

            HttpContext.Current.Items["urlwasrewritten"] = true;

        }

        public static bool UrlWasReWritten()
        {
            if (HttpContext.Current.Items["urlwasrewritten"] != null)
            {
                return true;
            }

            return false;

        }


        #endregion


        //public static string GetRoleCookieName()
        //{
        //    SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
        //    return GetRoleCookieName(siteSettings);
        //}

        public static string GetRoleCookieName(SiteSettings siteSettings)
        {
            String hostName = WebUtils.GetHostName();
            if (WebConfigSettings.UseRelatedSiteMode)
            {
                return hostName + "portalroles";
            }

            if (siteSettings == null) { return hostName + "portalroles1"; }

            return hostName + "portalroles" + siteSettings.SiteId.ToInvariantString();

        }

        public static string GetCssCacheCookieName(SiteSettings siteSettings)
        {
            if (siteSettings == null) { return "csscache"; }

            return "csscache" + siteSettings.SiteId.ToInvariantString();

        }

        public static void RedirectToSignOut()
        {
            if (HttpContext.Current == null) { return; }
            if (HttpContext.Current.Request == null) { return; }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Redirect(SiteUtils.GetNavigationSiteRoot() + "/Logoff.aspx", true);

        }

        #region Current User

        public static SiteUser GetCurrentSiteUser()
        {
            bool bypassAuthCheck = false;

            SiteUser currentUser = GetCurrentSiteUser(bypassAuthCheck);

            if ((currentUser != null)&&(currentUser.IsLockedOut))
            {
                RedirectToSignOut();
                return null;
            }

            return currentUser;
            
        }

        public static SiteUser GetCurrentSiteUser(bool bypassAuthCheck)
        {
            if (HttpContext.Current == null) return null;

           
            if (
                bypassAuthCheck
                || (HttpContext.Current.Request.IsAuthenticated)
                )
            {
                if (HttpContext.Current.Items["CurrentUser"] != null)
                {
                    return (SiteUser)HttpContext.Current.Items["CurrentUser"];
                }

                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if (siteSettings != null)
                {
                    SiteUser siteUser = new SiteUser(siteSettings, HttpContext.Current.User.Identity.Name);
                    if (siteUser.UserId > -1)
                    {
                        HttpContext.Current.Items["CurrentUser"] = siteUser;

                        return siteUser;
                    }
                }
            }

            return null;
        }

        public static string SuggestLoginNameFromEmail(int siteId, string email)
        {
            string login = email.Substring(0, email.IndexOf("@"));
            int offset = 1;
            while (SiteUser.LoginExistsInDB(siteId, login))
            {
                login = login + offset.ToInvariantString();
                offset += 1;
            }

            return login;
        }

        public static SiteUser CreateMinimalUser(SiteSettings siteSettings, string email, bool includeInMemberList, string adminComments)
        {
            if (siteSettings == null)
            {
                throw new ArgumentException("a valid siteSettings object is required for this method");
            }
            if (string.IsNullOrEmpty(email))
            {
                throw new ArgumentException("a valid email address is required for this method");
            }

            if (!Email.IsValidEmailAddressSyntax(email))
            {
                throw new ArgumentException("a valid email address is required for this method");
            }

            //first make sure he doesn't exist
            SiteUser siteUser = SiteUser.GetByEmail(siteSettings, email);
            if ((siteUser != null)&&(siteUser.UserGuid != Guid.Empty)) { return siteUser; }

            siteUser = new SiteUser(siteSettings);
            siteUser.Email = email;
            string login = SuggestLoginNameFromEmail(siteSettings.SiteId, email);
            //int offset = 1;
            //while (SiteUser.LoginExistsInDB(siteSettings.SiteId, login))
            //{
            //    login = login + offset.ToString(CultureInfo.InvariantCulture);
            //    offset += 1;
            //}

            siteUser.LoginName = login;
            siteUser.Name = login;
            siteUser.Password = SiteUser.CreateRandomPassword(siteSettings.MinRequiredPasswordLength + 2, WebConfigSettings.PasswordGeneratorChars);
            mojoMembershipProvider m = Membership.Provider as mojoMembershipProvider;
            if (m != null)
            {
                siteUser.Password = m.EncodePassword(siteUser.Password, siteSettings);
            }

            siteUser.ProfileApproved = true;
            siteUser.DisplayInMemberList = includeInMemberList;
            siteUser.PasswordQuestion = Resource.ManageUsersDefaultSecurityQuestion;
            siteUser.PasswordAnswer = Resource.ManageUsersDefaultSecurityAnswer;

            if (!string.IsNullOrEmpty(adminComments)) { siteUser.Comment = adminComments; }
            
            siteUser.Save();

            Role.AddUserToDefaultRoles(siteUser);

            return siteUser;

        }

        

        public static bool UserIsSiteEditor()
        {
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if(siteSettings != null)
            {
                return  (WebConfigSettings.UseRelatedSiteMode) && (WebUser.IsInRoles(siteSettings.SiteRootEditRoles));
            }

            return false;
        }

        public static bool UserCanEditModule(int moduleId)
        {
            if (HttpContext.Current == null) return false;
            if (!HttpContext.Current.Request.IsAuthenticated) return false;

            if (WebUser.IsAdminOrContentAdmin) return true;

            PageSettings currentPage = CacheHelper.GetCurrentPage();
            if (currentPage == null) return false;

            bool moduleFoundOnPage = false;
            foreach (Module m in currentPage.Modules)
            {
                if (m.ModuleId == moduleId) moduleFoundOnPage = true;
            }

            if (!moduleFoundOnPage) return false;

            if (WebUser.IsInRoles(currentPage.EditRoles)) return true;

            SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
            if (currentUser == null) return false;

            foreach (Module m in currentPage.Modules)
            {
                if (m.ModuleId == moduleId)
                {
                    if (m.EditUserId == currentUser.UserId) return true;
                    if (WebUser.IsInRoles(m.AuthorizedEditRoles)) return true;
                }
            }

            return false;

        }


        public static void TrackUserActivity()
        {
            if (HttpContext.Current == null) return;
            if (HttpContext.Current.Request == null) return;

            

            if (
                (HttpContext.Current.User.Identity.IsAuthenticated)
                &&(WebConfigSettings.TrackAuthenticatedRequests)
                )
            {
                if (
                (HttpContext.Current.Request.Path.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith(".js", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith(".gif", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith(".axd", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith("upgrade.aspx", StringComparison.InvariantCultureIgnoreCase))
                    || (HttpContext.Current.Request.Path.EndsWith("setup/default.aspx", StringComparison.InvariantCultureIgnoreCase))
                    )
                {
                    return;

                }

                //SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                //if (siteSettings != null)
                //{
                bool bypassAuthCheck = false;
                SiteUser siteUser = GetCurrentSiteUser(bypassAuthCheck);
                    //SiteUser siteUser = new SiteUser(siteSettings, HttpContext.Current.User.Identity.Name);
                    if ((siteUser != null) && (siteUser.UserId > -1))
                    {
                        siteUser.UpdateLastActivityTime();

                        if (HttpContext.Current.Request == null) return;

                        if (WebConfigSettings.TrackIPForAuthenticatedRequests)
                        {
                            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                            if (siteSettings == null) return;

                            // track user ip address
                            UserLocation userLocation = new UserLocation(
                                siteUser.UserGuid,
                                GetIP4Address());

                            userLocation.SiteGuid = siteSettings.SiteGuid;
                            userLocation.Hostname = HttpContext.Current.Request.UserHostName;
                            userLocation.Save();


                        }
                    }
                //}
            }

        }


        #endregion

        public static void QueueIndexing()
        {
            if (WebConfigSettings.DisableSearchIndex) { return; }

            if (!WebConfigSettings.IsSearchIndexingNode) { return;}

            if (IndexWriterTask.IsRunning()) { return; }

            IndexWriterTask indexWriter = new IndexWriterTask();
            
            indexWriter.StoreContentForResultsHighlighting = WebConfigSettings.EnableSearchResultsHighlighting;

            // Commented out 2009-01-24
            // seems to cause errors for some languages if we localize this
            // perhaps because the background thread is not running on the same culture as the
            // web ui which is driven by browser language preferecne.
            // if we do localize it we should localize to the site default culture, not the user's
            //indexWriter.TaskName = Resource.IndexWriterTaskName;
            //indexWriter.StatusCompleteMessage = Resource.IndexWriterTaskCompleteMessage;
            //indexWriter.StatusQueuedMessage = Resource.IndexWriterTaskQueuedMessage;
            //indexWriter.StatusStartedMessage = Resource.IndexWriterTaskStartedMessage;
            //indexWriter.StatusRunningMessage = Resource.IndexWriterTaskRunningFormatString;

            indexWriter.QueueTask();

            WebTaskManager.StartOrResumeTasks();

        }

        public static String GetFullPathToThemeFile()
        {
            string pathToFile = null;

            if (HttpContext.Current != null)
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                pathToFile = HttpContext.Current.Server.MapPath(
                    "~/Data/Sites/" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "/skins/"
                    + siteSettings.Skin + "/theme.skin");

            }

            return pathToFile;
        }


        public static int GetCurrentPageDepth(SiteMapNode rootNode)
        {
            
            PageSettings pageSettings = CacheHelper.GetCurrentPage();
            if ((pageSettings == null)
                ||(pageSettings.ParentId == -1)
                )
            {
                return 0;
            }

            SiteMapNode currentPageNode = GetSiteMapNodeForPage(rootNode, pageSettings);
            if (currentPageNode == null) { return 0; }

            if(!(currentPageNode is mojoSiteMapNode)){ return 0;}

            mojoSiteMapNode node = currentPageNode as mojoSiteMapNode;
            return node.Depth;
            
            
        }

        public static String GetActivePageValuePath(SiteMapNode rootNode, int offSet)
        {
            String valuePath = String.Empty;

            PageSettings pageSettings = CacheHelper.GetCurrentPage();

            SiteMapNode currentPageNode = GetSiteMapNodeForPage(rootNode, pageSettings);
            if (currentPageNode == null) { return string.Empty; }

            if (!(currentPageNode is mojoSiteMapNode)) { return string.Empty; }

            mojoSiteMapNode node = currentPageNode as mojoSiteMapNode;


            valuePath = node.PageGuid.ToString();

            while ((node != null) && (node.ParentId > -1))
            {
                if (node.ParentNode != null)
                {
                    node = node.ParentNode as mojoSiteMapNode;

                    valuePath = node.PageGuid.ToString() + "|" + valuePath;
                }
                
            }

            
            if (offSet > 0)
            {
                for (int i = 1; i <= offSet; i++)
                {
                    if (valuePath.IndexOf("|") > -1)
                    {
                        valuePath = valuePath.Remove(0, valuePath.IndexOf("|") + 1);
                    }

                }

            }

            return valuePath;
        }

        /// <summary>
        /// this overload was added to supp-ort menu highlighting on physical .aspx pages added to the menu
        /// </summary>
        /// <param name="rootNode"></param>
        /// <param name="offSet"></param>
        /// <param name="currentUrl"></param>
        /// <returns></returns>
        public static String GetActivePageValuePath(SiteMapNode rootNode, int offSet, string currentUrl)
        {
            string valuePath = string.Empty;

            SiteMapNode currentPageNode = GetSiteMapNodeForPage(rootNode, currentUrl);
            if (currentPageNode == null) { return string.Empty; }

            if (!(currentPageNode is mojoSiteMapNode)) { return string.Empty; }

            mojoSiteMapNode node = currentPageNode as mojoSiteMapNode;


            valuePath = node.PageGuid.ToString();

            while ((node != null) && (node.ParentId > -1))
            {
                if (node.ParentNode != null)
                {
                    node = node.ParentNode as mojoSiteMapNode;

                    valuePath = node.PageGuid.ToString() + "|" + valuePath;
                }

            }


            if (offSet > 0)
            {
                for (int i = 1; i <= offSet; i++)
                {
                    if (valuePath.IndexOf("|") > -1)
                    {
                        valuePath = valuePath.Remove(0, valuePath.IndexOf("|") + 1);
                    }

                }

            }

            return valuePath;


           // return valuePath;
        }
        

        //public static String GetTopParentUrlForPageMenu(SiteMapNode rootNode)
        //{
        //    //String pageUrl = String.Empty;

        //    PageSettings pageSettings = CacheHelper.GetCurrentPage();

        //    SiteMapNode currentPageNode = GetSiteMapNodeForPage(rootNode, pageSettings);
        //    if (currentPageNode == null) { return string.Empty; }

        //    SiteMapNode topMostParentNode = GetTopLevelParentNode(currentPageNode);
        //    if (topMostParentNode == null) { return string.Empty; }

        //    return topMostParentNode.Url;

            

            
        //}

        

        public static String GetPageMenuActivePageValuePath(SiteMapNode rootNode)
        {

            String valuePath = GetActivePageValuePath(rootNode, 0);
            
            // need to remove the topmost level from value path
            // which is from the beginning to the first separator
            if (valuePath.IndexOf("|") > -1)
            {
                valuePath = valuePath.Remove(0, valuePath.IndexOf("|") + 1);
            }


            return valuePath;
        }


        public static bool TopPageHasChildren(SiteMapNode rootNode)
        {
            return TopPageHasChildren(rootNode, 0);
        }

        public static mojoSiteMapNode GetCurrentPageSiteMapNode(SiteMapNode rootNode)
        {
            if (rootNode == null) { return null; }
            PageSettings currentPage = CacheHelper.GetCurrentPage();
            if (currentPage == null) { return null; }

            return GetSiteMapNodeForPage(rootNode, currentPage);

        }

        public static mojoSiteMapNode GetSiteMapNodeForPage(SiteMapNode rootNode, PageSettings pageSettings)
        {
            if (rootNode == null) { return null; }
            if (pageSettings == null) { return null; }
            if(!(rootNode is mojoSiteMapNode)){ return null;}

            foreach (SiteMapNode childNode in rootNode.ChildNodes)
            {
                if (!(childNode is mojoSiteMapNode)) { return null; }

                mojoSiteMapNode node = childNode as mojoSiteMapNode;
                if (node.PageId == pageSettings.PageId) { return node; }

                mojoSiteMapNode foundNode = GetSiteMapNodeForPage(node, pageSettings);
                if (foundNode != null) { return foundNode; }


            }

            return null;

        }

        /// <summary>
        /// this overload was added to implement support for menu highlighting in inline code .aspx pages
        /// added to the menu
        /// </summary>
        /// <param name="rootNode"></param>
        /// <param name="currentUrl"></param>
        /// <returns></returns>
        public static mojoSiteMapNode GetSiteMapNodeForPage(SiteMapNode rootNode, string currentUrl)
        {
            if (rootNode == null) { return null; }
            if (string.IsNullOrEmpty(currentUrl)) { return null; }
            if (!(rootNode is mojoSiteMapNode)) { return null; }

            foreach (SiteMapNode childNode in rootNode.ChildNodes)
            {
                if (!(childNode is mojoSiteMapNode)) { return null; }

                mojoSiteMapNode node = childNode as mojoSiteMapNode;
                if (string.Equals(node.Url.Replace("~", string.Empty),currentUrl, StringComparison.InvariantCultureIgnoreCase)) { return node; }

                mojoSiteMapNode foundNode = GetSiteMapNodeForPage(node, currentUrl);
                if (foundNode != null) { return foundNode; }


            }

            return null;

        }

        public static mojoSiteMapNode GetTopLevelParentNode(SiteMapNode siteMapNode)
        {
            if (siteMapNode == null) { return null; }
            if (!(siteMapNode is mojoSiteMapNode)) { return null; }

            mojoSiteMapNode node = siteMapNode as mojoSiteMapNode;

            if (node.ParentId < 0) { return node; }

            while ((node != null)&&(node.ParentId > -1))
            {
                if (node.ParentNode != null)
                {
                    node = node.ParentNode as mojoSiteMapNode;
                }
                else
                {
                    return node;
                }
            }

            return node;


        }


        public static bool NodeHasVisibleChildrenAtDepth(mojoSiteMapNode node, int depth)
        {
            bool recurse = true;
            return NodeHasVisibleChildrenAtDepth(node, depth, recurse);
        }


        public static bool NodeHasVisibleChildrenAtDepth(mojoSiteMapNode node, int depth, bool recurse)
        {
            foreach (SiteMapNode cNode in node.ChildNodes)
            {
                if (!(cNode is mojoSiteMapNode)) { return false; }

                mojoSiteMapNode childNode = cNode as mojoSiteMapNode;
                if (childNode.Depth >= depth)
                {
                    if ((childNode.IncludeInMenu)&&(WebUser.IsInRoles(childNode.ViewRoles))) { return true; }
                }
                else
                {
                    if (recurse)
                    {
                        if (NodeHasVisibleChildrenAtDepth(childNode, depth, recurse)) { return true; }
                    }
                }
                
            }

            return false;
        }

        /// <summary>
        /// A helper method for determining if the top level page has children at this offsetlevel
        /// used to determine whether to show or hide left or right div if it contains a menu.
        /// </summary>
        /// <param name="startingNodeOffset"></param>
        /// <returns></returns>
        public static bool TopPageHasChildren(SiteMapNode rootNode, int startingNodeOffset)
        {
            if (rootNode == null) { return false; }

            PageSettings pageSettings = CacheHelper.GetCurrentPage();
            
            if (pageSettings == null) return false;

            if (
                (pageSettings.ParentId == -1)
                && (startingNodeOffset > 0)
                )
            {
                return false;
            }

            if (
                (pageSettings.ParentId > -1)
                && (pageSettings.IncludeInMenu) //added 2009-05-06
                && (startingNodeOffset == 0)
                )
            {
                return true;
            }

            mojoSiteMapNode currentPageNode = GetSiteMapNodeForPage(rootNode, pageSettings);
            if (currentPageNode == null) { return false; }

            if (startingNodeOffset >= 2)
            {
                if ((currentPageNode.Depth >= startingNodeOffset)&&(currentPageNode.IncludeInMenu)) { return true; }

                bool recurse = false;
                mojoSiteMapNode parent;

                if (NodeHasVisibleChildrenAtDepth(currentPageNode, startingNodeOffset, recurse)) { return true; }

                if (currentPageNode.ParentNode != null)
                {
                    parent = currentPageNode.ParentNode as mojoSiteMapNode;
                    if (parent.Depth >= startingNodeOffset)
                    {
                        return NodeHasVisibleChildrenAtDepth(parent, startingNodeOffset, recurse);
                    }
                }

                return false;

            }

            mojoSiteMapNode topParent = GetTopLevelParentNode(currentPageNode);
            if (topParent == null) { return false; }

            if (NodeHasVisibleChildrenAtDepth(topParent, startingNodeOffset)) { return true; }

            return false;

            
        }

        public static String GetStartUrlForPageMenu(SiteMapNode rootNode, int startingNodeOffset)
        {
           
            PageSettings pageSettings = CacheHelper.GetCurrentPage();

            SiteMapNode currentPageNode = GetSiteMapNodeForPage(rootNode, pageSettings);
            if (currentPageNode == null) { return string.Empty; }

            if (startingNodeOffset == 0) 
            {
                SiteMapNode startingNode = GetTopLevelParentNode(currentPageNode);
                if (startingNode == null) { return string.Empty; }

                return startingNode.Url; 
            }

            //work our way up from the current page to the parent node at the offset depth
            mojoSiteMapNode n = currentPageNode as mojoSiteMapNode;

            while ((n.ParentNode != null) && (n.Depth > startingNodeOffset))
            {
                n = n.ParentNode as mojoSiteMapNode;
            }

            
            return n.Url;


        }

        

        

        //public static bool IsContentPage()
        //{
            
        //    if (HttpContext.Current != null)
        //    {
        //        String rawUrl = HttpContext.Current.Request.RawUrl;
        //        if ((HttpContext.Current.Request.RawUrl.Contains("Login.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("Register.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("ChangePassword.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("RecoverPassword.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("UserProfile.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("SiteMail"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("MyPage.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("SearchResults.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("Admin"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("Edit.aspx"))
        //            || (HttpContext.Current.Request.RawUrl.Contains("SiteMap.aspx"))
        //            )
        //        {
        //            return false;
        //        }
                
        //    }

        //    return true;

        //}

        //// TODO: this really shouldn't be here as it is specific to mojoportal.com
        //// purpose is to not show unsecure image on ssl pages
        //public static string GetSiteScoreImageMarkup()
        //{

        //    string markup = "<img src='http://sitescore.silktide.com/index.php?siteScoreUrl=http%3A%2F%2Fwww.mojoportal.com' "
        //        + "alt='Silktide Sitescore for this website' style='border: 0' />";

        //    if (HttpContext.Current != null)
        //    {
        //        if (HttpContext.Current.Request.ServerVariables["HTTPS"] == "on")
        //        {
        //            markup = String.Empty;
        //        }
        //    }

        //    return markup;

        //}

        public static string GetProfileLink(object objUserId, object userName)
        {
            string result = string.Empty;
            if (objUserId != null)
            {
                int userId = Convert.ToInt32(objUserId, CultureInfo.InvariantCulture);

                if (userName.ToString().Length > 0)
                {
                    result = "<a  href='" + GetNavigationSiteRoot() + "/ProfileView.aspx?userid="
                        + userId.ToString(CultureInfo.InvariantCulture) + "'>" + userName.ToString() + "</a>";
                }
            }

            return result;

        }

        public static string GetProfileLink(Page page, object objUserId, object userName)
        {
            string result = string.Empty;
            if (objUserId != null)
            {
                int userId = Convert.ToInt32(objUserId, CultureInfo.InvariantCulture);

                if (userName.ToString().Length > 0)
                {
                    result = "<a  href='"
                        + GetNavigationSiteRoot() 
                        + "/ProfileView.aspx?userid="
                        + userId.ToInvariantString() + "'>"
                        + userName.ToString() + "</a>";
                }
            }

            return result;

        }

        public static string GetProfileAvatarLink(
            Page page, 
            object objUserId, 
            int siteId,
            String avatar,
            String toolTip)
        {
            string result = string.Empty;
            if (objUserId != null)
            {
                int userId = Convert.ToInt32(objUserId);

               
                    if ((avatar == null) || (avatar == String.Empty))
                    {
                        avatar = "blank.gif";
                    }
                    String avaterImageMarkup = "<img title='" + toolTip 
                        + "'  alt='" + toolTip + "' src='"
                        + page.ResolveUrl("~/Data/Sites/" + siteId.ToString(CultureInfo.InvariantCulture) + "/useravatars/" + avatar) 
                        + "' />";

                    result = "<a title='" + toolTip + "' href='"
                        + GetNavigationSiteRoot()
                        + "/ProfileView.aspx?userid="
                        + userId.ToString(CultureInfo.InvariantCulture) + "'>"
                        + avaterImageMarkup + "</a>";
                
            }

            return result;

        }

        public static string GetGmapApiKey()
        {
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings.GmapApiKey.Length > 0)
                return siteSettings.GmapApiKey;

            return WebConfigSettings.GoogleMapsAPIKey;


        }

        public static string GetBingApiId()
        {
            if (WebConfigSettings.BingAPIId.Length > 0)
            {
                return WebConfigSettings.BingAPIId;

            }
            else
            {
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if (siteSettings != null)
                {
                    return siteSettings.BingAPIId;
                }

            }

            return string.Empty;

        }

        public static string GetSearchDomain()
        {
            if (WebConfigSettings.CustomSearchDomain.Length > 0)
            {
                return WebConfigSettings.CustomSearchDomain.Trim();

            }
            else
            {
                return WebUtils.GetHostName();
            }
        }

        public static string GetGoogleCustomSearchId()
        {
            if (WebConfigSettings.GoogleCustomSearchId.Length > 0)
            {
                return WebConfigSettings.GoogleCustomSearchId;
            }
            
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings != null) { return siteSettings.GoogleCustomSearchId; }

         
            return string.Empty;

        }

        public static string GetPrimarySearchProvider()
        {
            
            if (WebConfigSettings.PrimarySearchEngine.Length > 0)
            {
                return WebConfigSettings.PrimarySearchEngine;
            }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings != null) 
            { 
                string result = siteSettings.PrimarySearchEngine;
                switch (result)
                {
                    case "bing":
                        if (siteSettings.BingAPIId.Length > 0) { return result; }
                        break;

                    case "google":
                        if (siteSettings.GoogleCustomSearchId.Length > 0) { return result; }
                        break;

                    default:
                        return "internal";
                       
                }
            }

            return "internal";
            
        }

        public static bool ShowAlternateSearchIfConfigured()
        {
            if (WebConfigSettings.ShowAlternateSearchIfConfigured) { return true; }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings != null) { return siteSettings.ShowAlternateSearchIfConfigured; }

            return false;
        }

        public static CultureInfo GetDefaultCulture()
        {
            
            //if (WebConfigSettings.UseCultureOverride)
            //{
                SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
                if ((siteSettings != null) && (siteSettings.SiteId > -1))
                {
                    string siteCultureKey = "site" + siteSettings.SiteId.ToString(CultureInfo.InvariantCulture) + "culture";

                    if (ConfigurationManager.AppSettings[siteCultureKey] != null)
                    {
                        try
                        {
                            string cultureName = ConfigurationManager.AppSettings[siteCultureKey];

                            // change these neutral cultures which cannot be used to reasonable specific cultures
                            if (cultureName == "zh-CHS") { cultureName = "zh-CN"; }
                            if (cultureName == "zh-CHT") { cultureName = "zh-HK"; }

                            CultureInfo siteCulture = new CultureInfo(cultureName);
                            return siteCulture;
                        }
                        catch { }
                    }
                }
            //}

            return ResourceHelper.GetDefaultCulture();

        }

        public static Guid GetDefaultCountry()
        {

            // US
            Guid defaultCountry = new Guid("a71d6727-61e7-4282-9fcb-526d1e7bc24f");

            // TODO: add config setting, siteSetting ?

            return defaultCountry;

        }

        public static string GetTimeZoneLabel(double timeOffset)
        {
            string key = "TZ" + timeOffset.ToString(CultureInfo.InvariantCulture).Replace(".", string.Empty).Replace("-", "minus");
            return ResourceHelper.GetResourceString("TimeZoneResources", key);

        }

        public static List<TimeZoneInfo> GetTimeZoneList()
        {
            if (HttpContext.Current == null) return null;

            List<TimeZoneInfo> timeZones = HttpContext.Current.Items["tzlist"] as List<TimeZoneInfo>;
            if (timeZones == null)
            {
                if (WebConfigSettings.CacheTimeZoneList)
                {
                    timeZones = CacheHelper.GetTimeZones();
                }
                else
                {
                    timeZones = DateTimeHelper.GetTimeZoneList();
                }

                if (timeZones != null) { HttpContext.Current.Items["tzlist"] = timeZones; }
            }
            return timeZones;
        }

        public static TimeZoneInfo GetTimeZone(string id)
        {
            if (string.IsNullOrEmpty(id)) { return null; }
            List<TimeZoneInfo> timeZones = GetTimeZoneList();
            return DateTimeHelper.GetTimeZone(timeZones, id);
        }

        
        public static TimeZoneInfo GetSiteTimeZone()
        {
#if MONO
            return null;
#endif
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return null; }
            return GetTimeZone(siteSettings.TimeZoneId);

        }

        public static TimeZoneInfo GetUserTimeZone()
        {
#if !MONO
            SiteUser siteUser = GetCurrentSiteUser();
            if ((siteUser != null)&&(siteUser.TimeZoneId.Length > 0))
            {
                return GetTimeZone(siteUser.TimeZoneId);
            }
#endif

            return GetSiteTimeZone();
        }

        private static Double GetSiteTimeZoneOffset()
        {
            Double timeOffset = 0;
            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();
            if (siteSettings == null) { return timeOffset; }

            string cacheKey = "sitetzoffset_" + siteSettings.SiteId.ToInvariantString();
            object o = HttpContext.Current.Items[cacheKey];

            try
            {
                if (o != null)
                {
                    return Convert.ToDouble(o);
                }
                else
                {
                    TimeZoneInfo tz = GetTimeZone(siteSettings.TimeZoneId);
                    if (tz == null) { return timeOffset; }
                    Double siteTimeOffset = tz.GetUtcOffset(DateTime.UtcNow).TotalHours;
                    HttpContext.Current.Items[cacheKey] = siteTimeOffset;
                    return siteTimeOffset;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return timeOffset;
            }


        }

        public static double GetUserTimeOffset()
        {
#if MONO
            Double timeOffset = DateTimeHelper.GetPreferredGmtOffset();
#else
            Double timeOffset = 0;
#endif
            if (HttpContext.Current == null) { return timeOffset; }
            if (HttpContext.Current.Request == null) { return timeOffset; }

            
            if (!HttpContext.Current.Request.IsAuthenticated)
            {
#if MONO
                return timeOffset;
#else
                return GetSiteTimeZoneOffset();
#endif
            }
                
            SiteUser siteUser = GetCurrentSiteUser();
            if (siteUser != null)
            {
#if MONO
                timeOffset = siteUser.TimeOffsetHours;
#else
               
                if (siteUser.TimeZoneId.Length == 0) { return siteUser.TimeOffsetHours; }
                string cacheKey = "tzoffset_" + siteUser.UserId.ToInvariantString();
                object o = HttpContext.Current.Items[cacheKey];
                
                try
                {
                    if (o != null)
                    {
                        return Convert.ToDouble(o);
                    }
                    else
                    {
                        TimeZoneInfo tz = GetTimeZone(siteUser.TimeZoneId);
                        if (tz == null) { return siteUser.TimeOffsetHours; }
                        Double userTimeOffset = tz.GetUtcOffset(DateTime.UtcNow).TotalHours;
                        HttpContext.Current.Items[cacheKey] = userTimeOffset;
                        return userTimeOffset;
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    return siteUser.TimeOffsetHours;
                }
#endif

            }

            return timeOffset;

        }


        public static CommerceConfiguration GetCommerceConfig()
        {
            if (HttpContext.Current == null) return null;

            if (HttpContext.Current.Items["commerceConfig"] != null)
            {
                return (CommerceConfiguration)HttpContext.Current.Items["commerceConfig"];
            }

            SiteSettings siteSettings = CacheHelper.GetCurrentSiteSettings();

            if ((siteSettings == null) || (siteSettings.SiteGuid == Guid.Empty)) return null;

            CommerceConfiguration commerceConfig = new CommerceConfiguration(siteSettings);

            HttpContext.Current.Items.Add("commerceConfig", commerceConfig);

            return commerceConfig;


        }


    }
}
